2021-07-19 04:02:46 +05:30
|
|
|
import AVKit
|
|
|
|
import Logging
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlayerViewController: UIViewController {
|
|
|
|
var playerLoaded = false
|
2021-10-28 22:44:55 +05:30
|
|
|
var navigationModel: NavigationModel!
|
2021-09-25 13:48:22 +05:30
|
|
|
var playerModel: PlayerModel!
|
2021-07-19 04:02:46 +05:30
|
|
|
var playerViewController = AVPlayerViewController()
|
|
|
|
|
2021-11-08 02:21:22 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
var aspectRatio: Double? {
|
|
|
|
let ratio = Double(playerViewController.videoBounds.width) / Double(playerViewController.videoBounds.height)
|
2021-11-07 23:23:00 +05:30
|
|
|
|
2021-11-08 02:21:22 +05:30
|
|
|
guard ratio.isFinite else {
|
|
|
|
return VideoPlayerView.defaultAspectRatio // swiftlint:disable:this implicit_return
|
|
|
|
}
|
2021-11-07 23:23:00 +05:30
|
|
|
|
2021-11-08 02:21:22 +05:30
|
|
|
return [ratio, 1.0].max()!
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-07 23:23:00 +05:30
|
|
|
|
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-10-28 22:44:55 +05:30
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
if !playerViewController.isBeingPresented, !playerViewController.isBeingDismissed {
|
|
|
|
present(playerViewController, animated: false)
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-19 04:02:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func loadPlayer() {
|
2021-08-18 03:30:53 +05:30
|
|
|
guard !playerLoaded else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
playerModel.controller = self
|
2021-09-25 13:48:22 +05:30
|
|
|
playerViewController.player = playerModel.player
|
2021-10-06 01:50:09 +05:30
|
|
|
playerViewController.allowsPictureInPicturePlayback = true
|
|
|
|
playerViewController.delegate = self
|
2021-07-19 04:02:46 +05:30
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-10-06 01:50:09 +05:30
|
|
|
playerModel.avPlayerViewController = playerViewController
|
2021-11-03 04:32:02 +05:30
|
|
|
playerViewController.customInfoViewControllers = [
|
|
|
|
infoViewController([.related], title: "Related"),
|
|
|
|
infoViewController([.playingNext, .playedPreviously], title: "Playing Next")
|
|
|
|
]
|
2021-07-19 04:02:46 +05:30
|
|
|
#else
|
2021-08-18 03:30:53 +05:30
|
|
|
embedViewController()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-18 03:42:59 +05:30
|
|
|
#if os(tvOS)
|
2021-11-03 04:32:02 +05:30
|
|
|
func infoViewController(
|
|
|
|
_ sections: [NowPlayingView.ViewSection],
|
|
|
|
title: String
|
|
|
|
) -> UIHostingController<AnyView> {
|
2021-10-06 01:50:09 +05:30
|
|
|
let controller = UIHostingController(rootView:
|
|
|
|
AnyView(
|
2021-11-03 04:32:02 +05:30
|
|
|
NowPlayingView(sections: sections, inInfoViewController: true)
|
2021-10-14 03:35:19 +05:30
|
|
|
.frame(maxHeight: 600)
|
2021-10-06 01:50:09 +05:30
|
|
|
.environmentObject(playerModel)
|
|
|
|
)
|
2021-08-18 03:42:59 +05:30
|
|
|
)
|
|
|
|
|
2021-11-03 04:32:02 +05:30
|
|
|
controller.title = title
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
|
|
return controller
|
2021-08-18 03:42:59 +05:30
|
|
|
}
|
|
|
|
#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 {
|
2021-10-28 22:44:55 +05:30
|
|
|
true
|
2021-08-18 03:30:53 +05:30
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {}
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-07-30 03:58:28 +05:30
|
|
|
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
|
2021-07-19 04:02:46 +05:30
|
|
|
dismiss(animated: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(
|
|
|
|
_: AVPlayerViewController,
|
2021-08-02 02:55:50 +05:30
|
|
|
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
|
2021-10-06 01:50:09 +05:30
|
|
|
) {}
|
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
|
|
|
|
2021-10-28 22:44:55 +05:30
|
|
|
func playerViewController(
|
|
|
|
_ playerViewController: AVPlayerViewController,
|
|
|
|
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
|
|
|
|
) {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
if self.navigationModel.presentingChannel {
|
|
|
|
self.playerModel.playerNavigationLinkActive = true
|
|
|
|
} else {
|
|
|
|
self.playerModel.presentPlayer()
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-10-29 01:02:03 +05:30
|
|
|
if self.playerModel.playingInPictureInPicture {
|
2021-10-28 22:44:55 +05:30
|
|
|
self.present(playerViewController, animated: false) {
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
completionHandler(true)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
|
|
|
|
playerModel.playingInPictureInPicture = true
|
|
|
|
playerModel.playerNavigationLinkActive = false
|
|
|
|
}
|
2021-08-18 03:30:53 +05:30
|
|
|
|
2021-10-28 22:44:55 +05:30
|
|
|
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
|
|
|
|
playerModel.playingInPictureInPicture = false
|
|
|
|
}
|
2021-07-19 04:02:46 +05:30
|
|
|
}
|