fix(ios): apply PictureInPicture state on start (#3655)

This commit is contained in:
Krzysztof Moch 2024-04-05 10:37:00 +02:00 committed by GitHub
parent d6941392e0
commit 07f71c2fc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 89 additions and 14 deletions

2
examples/basic/index.js Normal file
View File

@ -0,0 +1,2 @@
// Without this file, the example will not build on physical devices
import './src/index';

View File

@ -935,11 +935,49 @@ PODS:
- React-Mapbuffer (0.74.0-rc.4): - React-Mapbuffer (0.74.0-rc.4):
- glog - glog
- React-debug - React-debug
- react-native-video (6.0.0-beta.6): - react-native-video (6.0.0-beta.8):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Codegen
- React-Core - React-Core
- react-native-video/Video (= 6.0.0-beta.6) - React-debug
- react-native-video/Video (6.0.0-beta.6): - React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- react-native-video/Video (= 6.0.0-beta.8)
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-video/Video (6.0.0-beta.8):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Codegen
- React-Core - React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- React-nativeconfig (0.74.0-rc.4) - React-nativeconfig (0.74.0-rc.4)
- React-NativeModulesApple (0.74.0-rc.4): - React-NativeModulesApple (0.74.0-rc.4):
- glog - glog
@ -1383,7 +1421,7 @@ SPEC CHECKSUMS:
React-jsitracing: 50e3ea936a199a2a7fcab922f156507c97f0b88c React-jsitracing: 50e3ea936a199a2a7fcab922f156507c97f0b88c
React-logger: 6004e0cf41b7e9714ca26b1648e5d76fcfd638b5 React-logger: 6004e0cf41b7e9714ca26b1648e5d76fcfd638b5
React-Mapbuffer: 9b163fa28e549d5f36f89a39a1145fcaf262d0d0 React-Mapbuffer: 9b163fa28e549d5f36f89a39a1145fcaf262d0d0
react-native-video: d340c162bf7974c2935fbeec0c5dea362f9dd74a react-native-video: 64df5d2bc3bbc028cb97d87b53e42583127a9b9e
React-nativeconfig: 3948d6fb6acfec364625cffbb1cf420346fb37c0 React-nativeconfig: 3948d6fb6acfec364625cffbb1cf420346fb37c0
React-NativeModulesApple: 46745aba687c1019983d56b6d5fa39265152f64f React-NativeModulesApple: 46745aba687c1019983d56b6d5fa39265152f64f
React-perflogger: 0d62c0261b6fd3920605850de91abc8135dd3ee9 React-perflogger: 0d62c0261b6fd3920605850de91abc8135dd3ee9

View File

@ -63,6 +63,10 @@ import React
_pipController?.delegate = self _pipController?.delegate = self
} }
func deinitPipController() {
_pipController = nil
}
func setPictureInPicture(_ isActive: Bool) { func setPictureInPicture(_ isActive: Bool) {
if _isActive == isActive { if _isActive == isActive {
return return

View File

@ -64,8 +64,20 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
private var _filterName: String! private var _filterName: String!
private var _filterEnabled = false private var _filterEnabled = false
private var _presentingViewController: UIViewController? private var _presentingViewController: UIViewController?
private var _pictureInPictureEnabled = false
private var _startPosition: Float64 = -1 private var _startPosition: Float64 = -1
private var _pictureInPictureEnabled = false {
didSet {
#if os(iOS)
if _pictureInPictureEnabled {
initPictureinPicture()
_playerViewController?.allowsPictureInPicturePlayback = true
} else {
_pip?.deinitPipController()
_playerViewController?.allowsPictureInPicturePlayback = false
}
#endif
}
}
/* IMA Ads */ /* IMA Ads */
private var _adTagUrl: String? private var _adTagUrl: String?
@ -144,6 +156,24 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
return _pictureInPictureEnabled return _pictureInPictureEnabled
} }
func initPictureinPicture() {
#if os(iOS)
_pip = RCTPictureInPicture({ [weak self] in
self?._onPictureInPictureEnter()
}, { [weak self] in
self?._onPictureInPictureExit()
}, { [weak self] in
self?.onRestoreUserInterfaceForPictureInPictureStop?([:])
})
if _playerLayer != nil && !_controls {
_pip?.setupPipController(_playerLayer)
}
#else
DebugLog("Picture in Picture is not supported on this platform")
#endif
}
init(eventDispatcher: RCTEventDispatcher!) { init(eventDispatcher: RCTEventDispatcher!) {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
#if USE_GOOGLE_IMA #if USE_GOOGLE_IMA
@ -153,13 +183,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
_eventDispatcher = eventDispatcher _eventDispatcher = eventDispatcher
#if os(iOS) #if os(iOS)
_pip = RCTPictureInPicture({ [weak self] in if _pictureInPictureEnabled {
self?._onPictureInPictureEnter() initPictureinPicture()
}, { [weak self] in _playerViewController?.allowsPictureInPicturePlayback = true
self?._onPictureInPictureExit() } else {
}, { [weak self] in _playerViewController?.allowsPictureInPicturePlayback = false
self?.onRestoreUserInterfaceForPictureInPictureStop?([:]) }
})
#endif #endif
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
@ -970,7 +999,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
viewController.view.frame = self.bounds viewController.view.frame = self.bounds
viewController.player = player viewController.player = player
if #available(tvOS 14.0, *) { if #available(tvOS 14.0, *) {
viewController.allowsPictureInPicturePlayback = true viewController.allowsPictureInPicturePlayback = _pictureInPictureEnabled
} }
return viewController return viewController
} }
@ -991,7 +1020,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
} }
self.layer.needsDisplayOnBoundsChange = true self.layer.needsDisplayOnBoundsChange = true
#if os(iOS) #if os(iOS)
if _pictureInPictureEnabled {
_pip?.setupPipController(_playerLayer) _pip?.setupPipController(_playerLayer)
}
#endif #endif
} }
} }