2021-10-28 17:14:55 +00:00
|
|
|
import AVKit
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class PictureInPictureDelegate: NSObject, AVPlayerViewPictureInPictureDelegate {
|
|
|
|
var playerModel: PlayerModel!
|
|
|
|
|
|
|
|
func playerViewShouldAutomaticallyDismissAtPicture(inPictureStart _: AVPlayerView) -> Bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewWillStartPicture(inPicture _: AVPlayerView) {
|
2021-11-02 23:02:02 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
|
|
self?.playerModel.playingInPictureInPicture = true
|
|
|
|
self?.playerModel.presentingPlayer = false
|
|
|
|
}
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewWillStopPicture(inPicture _: AVPlayerView) {
|
2021-11-02 23:02:02 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
|
|
self?.playerModel.playingInPictureInPicture = false
|
|
|
|
self?.playerModel.presentPlayer()
|
|
|
|
}
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func playerView(
|
|
|
|
_: AVPlayerView,
|
|
|
|
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void
|
|
|
|
) {
|
2021-11-02 23:02:02 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
|
|
self?.playerModel.presentingPlayer = true
|
|
|
|
}
|
2021-10-28 17:14:55 +00:00
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
}
|