Merge pull request #3221 from lrusso/master

Fixing the PIP mode on iOS
This commit is contained in:
Olivier Bouillet
2023-09-04 23:42:01 +02:00
committed by GitHub
2 changed files with 36 additions and 20 deletions

View File

@@ -4,15 +4,15 @@ import MediaAccessibility
import React
import Foundation
#if TARGET_OS_IOS
#if os(iOS)
class RCTPictureInPicture: NSObject, AVPictureInPictureControllerDelegate {
private var _onPictureInPictureStatusChanged: RCTDirectEventBlock?
private var _onRestoreUserInterfaceForPictureInPictureStop: RCTDirectEventBlock?
private var _onPictureInPictureStatusChanged: (() -> Void)? = nil
private var _onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)? = nil
private var _restoreUserInterfaceForPIPStopCompletionHandler:((Bool) -> Void)? = nil
private var _pipController:AVPictureInPictureController?
private var _isActive:Bool = false
init(_ onPictureInPictureStatusChanged: @escaping RCTDirectEventBlock, _ onRestoreUserInterfaceForPictureInPictureStop: @escaping RCTDirectEventBlock) {
init(_ onPictureInPictureStatusChanged: (() -> Void)? = nil, _ onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)? = nil) {
_onPictureInPictureStatusChanged = onPictureInPictureStatusChanged
_onRestoreUserInterfaceForPictureInPictureStop = onRestoreUserInterfaceForPictureInPictureStop
}
@@ -20,22 +20,20 @@ class RCTPictureInPicture: NSObject, AVPictureInPictureControllerDelegate {
func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
guard let _onPictureInPictureStatusChanged = _onPictureInPictureStatusChanged else { return }
_onPictureInPictureStatusChanged([ "isActive": NSNumber(value: true)])
_onPictureInPictureStatusChanged()
}
func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
guard let _onPictureInPictureStatusChanged = _onPictureInPictureStatusChanged else { return }
_onPictureInPictureStatusChanged([ "isActive": NSNumber(value: false)])
_onPictureInPictureStatusChanged()
}
func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
assert(_restoreUserInterfaceForPIPStopCompletionHandler == nil, "restoreUserInterfaceForPIPStopCompletionHandler was not called after picture in picture was exited.")
guard let _onRestoreUserInterfaceForPictureInPictureStop = _onRestoreUserInterfaceForPictureInPictureStop else { return }
_onRestoreUserInterfaceForPictureInPictureStop([:])
_onRestoreUserInterfaceForPictureInPictureStop()
_restoreUserInterfaceForPIPStopCompletionHandler = completionHandler
}
@@ -47,7 +45,6 @@ class RCTPictureInPicture: NSObject, AVPictureInPictureControllerDelegate {
}
func setupPipController(_ playerLayer: AVPlayerLayer?) {
guard playerLayer != nil && AVPictureInPictureController.isPictureInPictureSupported() && _isActive else { return }
// Create new controller passing reference to the AVPlayerLayer
_pipController = AVPictureInPictureController(playerLayer:playerLayer!)
_pipController?.delegate = self