1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 14:20:32 +05:30
yattee/Shared/Player/PlayerViewController.swift

135 lines
4.0 KiB
Swift
Raw Normal View History

2021-07-19 04:02:46 +05:30
import AVKit
import Logging
import SwiftUI
final class PlayerViewController: UIViewController {
var video: Video!
var playerLoaded = false
var player = AVPlayer()
2021-08-23 00:43:33 +05:30
var playerState: PlayerState!
var playbackState: PlaybackState!
2021-07-19 04:02:46 +05:30
var playerViewController = AVPlayerViewController()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2021-08-18 03:30:53 +05:30
loadPlayer()
2021-07-19 04:02:46 +05:30
2021-08-18 03:30:53 +05:30
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
2021-07-19 04:02:46 +05:30
try? AVAudioSession.sharedInstance().setActive(true)
}
override func viewDidDisappear(_ animated: Bool) {
#if os(iOS)
2021-08-18 03:30:53 +05:30
if !playerState.playingOutsideViewController {
2021-07-19 04:02:46 +05:30
playerViewController.player?.replaceCurrentItem(with: nil)
playerViewController.player = nil
2021-08-18 03:30:53 +05:30
try? AVAudioSession.sharedInstance().setActive(false)
2021-07-19 04:02:46 +05:30
}
#endif
super.viewDidDisappear(animated)
}
func loadPlayer() {
2021-08-23 00:43:33 +05:30
playerState = PlayerState()
playerState.playbackState = playbackState
2021-08-18 03:30:53 +05:30
guard !playerLoaded else {
return
}
2021-07-19 04:02:46 +05:30
playerState.player = player
playerViewController.player = playerState.player
playerState.loadVideo(video)
#if os(tvOS)
present(playerViewController, animated: false)
addItemDidPlayToEndTimeObserver()
2021-07-19 04:02:46 +05:30
#else
2021-08-18 03:30:53 +05:30
embedViewController()
#endif
playerViewController.allowsPictureInPicturePlayback = true
playerViewController.delegate = self
playerLoaded = true
}
#if os(tvOS)
func addItemDidPlayToEndTimeObserver() {
NotificationCenter.default.addObserver(
self,
selector: #selector(itemDidPlayToEndTime),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: nil
)
}
@objc func itemDidPlayToEndTime() {
playerViewController.dismiss(animated: true) {
self.dismiss(animated: false)
}
}
#else
2021-08-18 03:30:53 +05:30
func embedViewController() {
2021-07-19 04:02:46 +05:30
playerViewController.exitsFullScreenWhenPlaybackEnds = true
playerViewController.view.frame = view.bounds
addChild(playerViewController)
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
2021-08-18 03:30:53 +05:30
}
#endif
2021-07-19 04:02:46 +05:30
}
extension PlayerViewController: AVPlayerViewControllerDelegate {
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
true
}
2021-08-18 03:30:53 +05:30
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
false
}
2021-07-30 03:58:28 +05:30
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
2021-08-18 03:30:53 +05:30
playerState.playingOutsideViewController = false
2021-07-19 04:02:46 +05:30
dismiss(animated: false)
}
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
2021-07-19 04:02:46 +05:30
) {
2021-08-18 03:30:53 +05:30
playerState.playingOutsideViewController = true
2021-07-19 04:02:46 +05:30
}
func playerViewController(
_: AVPlayerViewController,
2021-07-30 03:58:28 +05:30
willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator
2021-07-19 04:02:46 +05:30
) {
2021-07-30 03:58:28 +05:30
coordinator.animate(alongsideTransition: nil) { context in
if !context.isCancelled {
2021-08-18 03:30:53 +05:30
self.playerState.playingOutsideViewController = false
2021-08-23 00:43:33 +05:30
#if os(iOS)
if self.traitCollection.verticalSizeClass == .compact {
self.dismiss(animated: true)
}
#endif
2021-07-30 03:58:28 +05:30
}
}
2021-07-19 04:02:46 +05:30
}
2021-08-18 03:30:53 +05:30
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerState.playingOutsideViewController = true
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerState.playingOutsideViewController = false
}
2021-07-19 04:02:46 +05:30
}