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

112 lines
3.3 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 playerLoaded = false
2021-09-25 13:48:22 +05:30
var playerModel: PlayerModel!
2021-07-19 04:02:46 +05:30
var playerViewController = AVPlayerViewController()
var shouldResume = false
2021-07-19 04:02:46 +05:30
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)
}
func loadPlayer() {
2021-08-18 03:30:53 +05:30
guard !playerLoaded else {
return
}
playerModel.controller = self
2021-09-25 13:48:22 +05:30
playerViewController.player = playerModel.player
playerViewController.allowsPictureInPicturePlayback = true
playerViewController.delegate = self
2021-07-19 04:02:46 +05:30
#if os(tvOS)
playerModel.avPlayerViewController = playerViewController
playerViewController.customInfoViewControllers = [playerQueueInfoViewController]
2021-07-19 04:02:46 +05:30
present(playerViewController, animated: false)
#else
2021-08-18 03:30:53 +05:30
embedViewController()
#endif
playerLoaded = true
}
#if os(tvOS)
var playerQueueInfoViewController: UIHostingController<AnyView> {
let controller = UIHostingController(rootView:
AnyView(
NowPlayingView(infoViewController: true)
2021-10-14 03:35:19 +05:30
.frame(maxHeight: 600)
.environmentObject(playerModel)
)
)
controller.title = "Playing Next"
return controller
}
#else
2021-08-18 03:30:53 +05:30
func embedViewController() {
2021-07-19 04:02:46 +05:30
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
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
shouldResume = playerModel.isPlaying
}
2021-07-30 03:58:28 +05:30
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
if shouldResume {
2021-10-17 04:18:58 +05:30
playerModel.play()
}
2021-07-19 04:02:46 +05:30
dismiss(animated: false)
}
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
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-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) {}
2021-08-18 03:30:53 +05:30
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {}
2021-07-19 04:02:46 +05:30
}