2021-07-18 22:32:46 +00:00
|
|
|
import AVKit
|
|
|
|
import Logging
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlayerViewController: UIViewController {
|
|
|
|
var video: Video!
|
|
|
|
|
|
|
|
var playerLoaded = false
|
|
|
|
var player = AVPlayer()
|
2021-08-22 19:13:33 +00:00
|
|
|
var playerState: PlayerState!
|
|
|
|
var playbackState: PlaybackState!
|
2021-07-18 22:32:46 +00:00
|
|
|
var playerViewController = AVPlayerViewController()
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
loadPlayer()
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
2021-07-18 22:32:46 +00:00
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
#if os(iOS)
|
2021-08-17 22:00:53 +00:00
|
|
|
if !playerState.playingOutsideViewController {
|
2021-07-18 22:32:46 +00:00
|
|
|
playerViewController.player?.replaceCurrentItem(with: nil)
|
|
|
|
playerViewController.player = nil
|
2021-08-17 22:00:53 +00:00
|
|
|
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(false)
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPlayer() {
|
2021-08-22 19:13:33 +00:00
|
|
|
playerState = PlayerState()
|
|
|
|
playerState.playbackState = playbackState
|
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
guard !playerLoaded else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
playerState.player = player
|
|
|
|
playerViewController.player = playerState.player
|
|
|
|
playerState.loadVideo(video)
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
present(playerViewController, animated: false)
|
2021-08-17 22:12:59 +00:00
|
|
|
|
|
|
|
addItemDidPlayToEndTimeObserver()
|
2021-07-18 22:32:46 +00:00
|
|
|
#else
|
2021-08-17 22:00:53 +00:00
|
|
|
embedViewController()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
playerViewController.allowsPictureInPicturePlayback = true
|
|
|
|
playerViewController.delegate = self
|
|
|
|
playerLoaded = true
|
|
|
|
}
|
|
|
|
|
2021-08-17 22:12:59 +00:00
|
|
|
#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-17 22:00:53 +00:00
|
|
|
func embedViewController() {
|
2021-07-18 22:32:46 +00:00
|
|
|
playerViewController.exitsFullScreenWhenPlaybackEnds = true
|
|
|
|
playerViewController.view.frame = view.bounds
|
|
|
|
|
|
|
|
addChild(playerViewController)
|
|
|
|
view.addSubview(playerViewController.view)
|
|
|
|
|
|
|
|
playerViewController.didMove(toParent: self)
|
2021-08-17 22:00:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension PlayerViewController: AVPlayerViewControllerDelegate {
|
|
|
|
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:28:28 +00:00
|
|
|
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
|
2021-08-17 22:00:53 +00:00
|
|
|
playerState.playingOutsideViewController = false
|
2021-07-18 22:32:46 +00:00
|
|
|
dismiss(animated: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(
|
|
|
|
_: AVPlayerViewController,
|
2021-08-01 21:25:50 +00:00
|
|
|
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
|
2021-07-18 22:32:46 +00:00
|
|
|
) {
|
2021-08-17 22:00:53 +00:00
|
|
|
playerState.playingOutsideViewController = true
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(
|
|
|
|
_: AVPlayerViewController,
|
2021-07-29 22:28:28 +00:00
|
|
|
willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator
|
2021-07-18 22:32:46 +00:00
|
|
|
) {
|
2021-07-29 22:28:28 +00:00
|
|
|
coordinator.animate(alongsideTransition: nil) { context in
|
|
|
|
if !context.isCancelled {
|
2021-08-17 22:00:53 +00:00
|
|
|
self.playerState.playingOutsideViewController = false
|
2021-08-22 19:13:33 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
if self.traitCollection.verticalSizeClass == .compact {
|
|
|
|
self.dismiss(animated: true)
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-29 22:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
2021-08-17 22:00:53 +00:00
|
|
|
|
|
|
|
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
|
|
|
|
playerState.playingOutsideViewController = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
|
|
|
|
playerState.playingOutsideViewController = false
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|