Fix PiP close on Mac

This commit is contained in:
Arkadiusz Fal 2023-05-22 22:48:11 +02:00
parent 16fb7087e3
commit c0053cf837
4 changed files with 9 additions and 11 deletions

View File

@ -31,7 +31,9 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
player.controls.objectWillChange.send() player.controls.objectWillChange.send()
} }
func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {} func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {
player.show()
}
func pictureInPictureController( func pictureInPictureController(
_: AVPictureInPictureController, _: AVPictureInPictureController,

View File

@ -143,14 +143,12 @@ import SwiftUI
} }
#else #else
struct AppleAVPlayerView: NSViewRepresentable { struct AppleAVPlayerView: NSViewRepresentable {
@State private var pictureInPictureDelegate = MacOSPiPDelegate()
func makeNSView(context _: Context) -> some NSView { func makeNSView(context _: Context) -> some NSView {
let view = AVPlayerView() let view = AVPlayerView()
view.player = PlayerModel.shared.avPlayerBackend.avPlayer view.player = PlayerModel.shared.avPlayerBackend.avPlayer
view.showsFullScreenToggleButton = true view.showsFullScreenToggleButton = true
view.allowsPictureInPicturePlayback = true view.allowsPictureInPicturePlayback = true
view.pictureInPictureDelegate = pictureInPictureDelegate view.pictureInPictureDelegate = MacOSPiPDelegate.shared
return view return view
} }

View File

@ -28,6 +28,7 @@ struct PlayerBackendView: View {
{ {
AppleAVPlayerView() AppleAVPlayerView()
} else if !avPlayerUsesSystemControls || } else if !avPlayerUsesSystemControls ||
player.playingInPictureInPicture ||
player.avPlayerBackend.isStartingPiP player.avPlayerBackend.isStartingPiP
{ {
AppleAVPlayerLayerView() AppleAVPlayerLayerView()

View File

@ -2,6 +2,8 @@ import AVKit
import Foundation import Foundation
final class MacOSPiPDelegate: NSObject, AVPlayerViewPictureInPictureDelegate { final class MacOSPiPDelegate: NSObject, AVPlayerViewPictureInPictureDelegate {
static let shared = MacOSPiPDelegate()
var playerModel: PlayerModel { .shared } var playerModel: PlayerModel { .shared }
func playerViewShouldAutomaticallyDismissAtPicture(inPictureStart _: AVPlayerView) -> Bool { func playerViewShouldAutomaticallyDismissAtPicture(inPictureStart _: AVPlayerView) -> Bool {
@ -16,19 +18,14 @@ final class MacOSPiPDelegate: NSObject, AVPlayerViewPictureInPictureDelegate {
} }
func playerViewWillStopPicture(inPicture _: AVPlayerView) { func playerViewWillStopPicture(inPicture _: AVPlayerView) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in playerModel.show()
self?.playerModel.playingInPictureInPicture = false playerModel.playingInPictureInPicture = false
self?.playerModel.show()
}
} }
func playerView( func playerView(
_: AVPlayerView, _: AVPlayerView,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void
) { ) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
self?.playerModel.show()
}
completionHandler(true) completionHandler(true)
} }
} }