yattee/Model/Player/PiPDelegate.swift

54 lines
1.7 KiB
Swift
Raw Normal View History

2022-05-20 21:23:14 +00:00
import AVKit
import Foundation
final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
var player: PlayerModel!
func pictureInPictureController(
_: AVPictureInPictureController,
failedToStartPictureInPictureWithError error: Error
) {
print(error.localizedDescription)
}
func pictureInPictureControllerWillStartPictureInPicture(_: AVPictureInPictureController) {}
2022-05-21 20:58:11 +00:00
func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) {
player?.playingInPictureInPicture = true
player?.avPlayerBackend.startPictureInPictureOnPlay = false
}
2022-05-20 21:23:14 +00:00
func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) {
2022-05-29 20:13:21 +00:00
guard let player = player else {
return
}
if player.avPlayerBackend.switchToMPVOnPipClose,
!player.currentItem.isNil {
DispatchQueue.main.async {
player.avPlayerBackend.switchToMPVOnPipClose = false
player.saveTime {
player.changeActiveBackend(from: .appleAVPlayer, to: .mpv)
2022-05-20 21:23:14 +00:00
}
}
}
2022-05-21 20:58:11 +00:00
2022-05-29 20:13:21 +00:00
player.playingInPictureInPicture = false
2022-05-20 21:23:14 +00:00
}
func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {}
func pictureInPictureController(
_: AVPictureInPictureController,
2022-05-21 20:58:11 +00:00
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
2022-05-29 20:13:21 +00:00
if !player.currentItem.isNil {
player?.show()
}
2022-05-29 14:38:37 +00:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
completionHandler(true)
}
2022-05-21 20:58:11 +00:00
}
2022-05-20 21:23:14 +00:00
}