1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Player/PiPDelegate.swift

72 lines
2.1 KiB
Swift
Raw Normal View History

2022-05-21 02:53:14 +05:30
import AVKit
2022-08-27 01:47:21 +05:30
import Defaults
2022-05-21 02:53:14 +05:30
import Foundation
2022-08-06 02:07:38 +05:30
import SwiftUI
2022-05-21 02:53:14 +05:30
final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
2023-05-21 02:19:10 +05:30
var player: PlayerModel { .shared }
2022-05-21 02:53:14 +05:30
func pictureInPictureController(
_: AVPictureInPictureController,
failedToStartPictureInPictureWithError error: Error
) {
print(error.localizedDescription)
}
func pictureInPictureControllerWillStartPictureInPicture(_: AVPictureInPictureController) {}
2022-05-22 02:28:11 +05:30
func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) {
2023-05-21 02:19:10 +05:30
player.play()
2022-08-27 01:47:21 +05:30
player.playingInPictureInPicture = true
player.avPlayerBackend.startPictureInPictureOnPlay = false
player.avPlayerBackend.startPictureInPictureOnSwitch = false
player.controls.objectWillChange.send()
2023-05-21 02:19:10 +05:30
if Defaults[.closePlayerOnOpeningPiP] { Delay.by(0.1) { self.player.hide() } }
2022-05-22 02:28:11 +05:30
}
2022-05-21 02:53:14 +05:30
func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) {
2022-05-30 01:43:21 +05:30
player.playingInPictureInPicture = false
2022-08-27 01:47:21 +05:30
player.controls.objectWillChange.send()
2022-05-21 02:53:14 +05:30
}
2023-05-23 02:18:11 +05:30
func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {
player.show()
}
2022-05-21 02:53:14 +05:30
func pictureInPictureController(
_: AVPictureInPictureController,
2022-05-22 02:28:11 +05:30
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
2023-05-21 02:19:10 +05:30
let wasPlaying = player.isPlaying
2022-05-31 00:30:53 +05:30
var delay = 0.0
#if os(iOS)
2022-08-06 02:07:38 +05:30
if !player.presentingPlayer {
2022-05-31 00:30:53 +05:30
delay = 0.5
}
2022-08-06 02:07:38 +05:30
if player.currentItem.isNil {
delay = 1
}
2022-05-31 00:30:53 +05:30
#endif
2022-06-08 02:57:48 +05:30
if !player.currentItem.isNil, !player.musicMode {
2023-05-21 02:19:10 +05:30
player.show()
2022-05-30 01:43:21 +05:30
}
2022-05-30 02:00:00 +05:30
2022-08-06 02:07:38 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
withAnimation(.linear(duration: 0.3)) {
self?.player.playingInPictureInPicture = false
}
2023-05-21 02:19:10 +05:30
if wasPlaying {
Delay.by(1) {
self?.player.play()
}
}
2022-05-29 20:08:37 +05:30
completionHandler(true)
}
2022-05-22 02:28:11 +05:30
}
2022-05-21 02:53:14 +05:30
}