yattee/Shared/Player/PlayerViewController.swift

112 lines
3.3 KiB
Swift
Raw Normal View History

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()
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
}
playerModel.controller = self
2021-09-25 08:18:22 +00:00
playerViewController.player = playerModel.player
playerViewController.allowsPictureInPicturePlayback = true
playerViewController.delegate = self
2021-07-18 22:32:46 +00:00
#if os(tvOS)
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
}
#if os(tvOS)
var playerQueueInfoViewController: UIHostingController<AnyView> {
let controller = UIHostingController(rootView:
AnyView(
NowPlayingView(infoViewController: true)
2021-10-13 22:05:19 +00:00
.frame(maxHeight: 600)
.environmentObject(playerModel)
)
)
controller.title = "Playing Next"
return controller
}
#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
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
shouldResume = playerModel.isPlaying
}
2021-07-29 22:28:28 +00:00
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
if shouldResume {
2021-10-16 22:48:58 +00:00
playerModel.play()
}
2021-07-18 22:32:46 +00:00
dismiss(animated: false)
}
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
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
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {}
2021-08-17 22:00:53 +00:00
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {}
2021-07-18 22:32:46 +00:00
}