2021-07-18 22:32:46 +00:00
|
|
|
import AVKit
|
|
|
|
import Logging
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlayerViewController: UIViewController {
|
|
|
|
var playerLoaded = false
|
2021-09-25 08:18:22 +00:00
|
|
|
var playerModel: PlayerModel!
|
2021-07-18 22:32:46 +00:00
|
|
|
var playerViewController = AVPlayerViewController()
|
2021-10-05 20:20:09 +00:00
|
|
|
var shouldResume = false
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPlayer() {
|
2021-08-17 22:00:53 +00:00
|
|
|
guard !playerLoaded else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
playerModel.controller = self
|
2021-09-25 08:18:22 +00:00
|
|
|
playerViewController.player = playerModel.player
|
2021-10-05 20:20:09 +00:00
|
|
|
playerViewController.allowsPictureInPicturePlayback = true
|
|
|
|
playerViewController.delegate = self
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-10-05 20:20:09 +00:00
|
|
|
playerModel.avPlayerViewController = playerViewController
|
|
|
|
playerViewController.customInfoViewControllers = [playerQueueInfoViewController]
|
2021-07-18 22:32:46 +00:00
|
|
|
present(playerViewController, animated: false)
|
|
|
|
#else
|
2021-08-17 22:00:53 +00:00
|
|
|
embedViewController()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
playerLoaded = true
|
|
|
|
}
|
|
|
|
|
2021-08-17 22:12:59 +00:00
|
|
|
#if os(tvOS)
|
2021-10-05 20:20:09 +00:00
|
|
|
var playerQueueInfoViewController: UIHostingController<AnyView> {
|
|
|
|
let controller = UIHostingController(rootView:
|
|
|
|
AnyView(
|
|
|
|
NowPlayingView(infoViewController: true)
|
2021-10-13 22:05:19 +00:00
|
|
|
.frame(maxHeight: 600)
|
2021-10-05 20:20:09 +00:00
|
|
|
.environmentObject(playerModel)
|
|
|
|
)
|
2021-08-17 22:12:59 +00:00
|
|
|
)
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
controller.title = "Playing Next"
|
|
|
|
|
|
|
|
return controller
|
2021-08-17 22:12:59 +00:00
|
|
|
}
|
|
|
|
#else
|
2021-08-17 22:00:53 +00:00
|
|
|
func embedViewController() {
|
2021-07-18 22:32:46 +00:00
|
|
|
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-10-05 20:20:09 +00:00
|
|
|
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
|
|
|
|
shouldResume = playerModel.isPlaying
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:28:28 +00:00
|
|
|
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
|
2021-10-05 20:20:09 +00:00
|
|
|
if shouldResume {
|
2021-10-16 22:48:58 +00:00
|
|
|
playerModel.play()
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
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-10-05 20:20:09 +00:00
|
|
|
) {}
|
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-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
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {}
|
2021-08-17 22:00:53 +00:00
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {}
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|