yattee/Shared/PlayerViewController.swift

87 lines
2.4 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 video: Video!
var playerLoaded = false
var playingFullScreen = false
var player = AVPlayer()
var playerState: PlayerState! = PlayerState()
var playerViewController = AVPlayerViewController()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !playerLoaded {
loadPlayer()
}
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try? AVAudioSession.sharedInstance().setActive(true)
}
override func viewDidDisappear(_ animated: Bool) {
#if os(iOS)
if !playingFullScreen {
playerViewController.player?.replaceCurrentItem(with: nil)
playerViewController.player = nil
}
#endif
super.viewDidDisappear(animated)
}
func loadPlayer() {
playerState.player = player
playerViewController.player = playerState.player
playerState.loadVideo(video)
#if os(tvOS)
present(playerViewController, animated: false)
#else
playerViewController.exitsFullScreenWhenPlaybackEnds = true
playerViewController.view.frame = view.bounds
addChild(playerViewController)
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
#endif
playerViewController.delegate = self
playerLoaded = true
}
}
extension PlayerViewController: AVPlayerViewControllerDelegate {
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
true
}
2021-07-29 22:28:28 +00:00
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
playingFullScreen = false
2021-07-18 22:32:46 +00:00
dismiss(animated: false)
}
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
2021-07-18 22:32:46 +00:00
) {
playingFullScreen = 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 {
self.playingFullScreen = false
}
}
2021-07-18 22:32:46 +00:00
}
}