@@ -68,14 +68,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
6868 if isPictureInPictureActive ( ) { return }
6969 if _enterPictureInPictureOnLeave {
7070 initPictureinPicture ( )
71- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
72- _playerViewController? . allowsPictureInPicturePlayback = true
73- }
7471 } else {
7572 _pip? . deinitPipController ( )
76- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
77- _playerViewController? . allowsPictureInPicturePlayback = false
78- }
7973 }
8074 }
8175 }
@@ -105,6 +99,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
10599 #endif
106100
107101 private var _pip : RCTPictureInPicture ?
102+ private var _isPictureInPictureActive = false
108103
109104 // Events
110105 @objc var onVideoLoadStart : RCTDirectEventBlock ?
@@ -138,19 +133,21 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
138133
139134 @objc
140135 func _onPictureInPictureEnter( ) {
141- onPictureInPictureStatusChanged ? ( [ " isActive " : NSNumber ( value : true ) ] )
136+ handlePictureInPictureEnter ( )
142137 }
143138
144139 @objc
145140 func _onPictureInPictureExit( ) {
146- onPictureInPictureStatusChanged ? ( [ " isActive " : NSNumber ( value : false ) ] )
141+ handlePictureInPictureExit ( )
147142 }
148143
149144 func handlePictureInPictureEnter( ) {
145+ _isPictureInPictureActive = true
150146 onPictureInPictureStatusChanged ? ( [ " isActive " : NSNumber ( value: true ) ] )
151147 }
152148
153149 func handlePictureInPictureExit( ) {
150+ _isPictureInPictureActive = false
154151 onPictureInPictureStatusChanged ? ( [ " isActive " : NSNumber ( value: false ) ] )
155152
156153 // To continue audio playback in backgroud we need to set
@@ -169,7 +166,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
169166
170167 func isPictureInPictureActive( ) -> Bool {
171168 #if os(iOS)
172- return _pip ? . _pipController ? . isPictureInPictureActive == true
169+ return _isPictureInPictureActive
173170 #else
174171 return false
175172 #endif
@@ -1211,9 +1208,6 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
12111208 }
12121209 }
12131210
1214- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
1215- viewController. allowsPictureInPicturePlayback = _enterPictureInPictureOnLeave
1216- }
12171211 return viewController
12181212 }
12191213
@@ -1244,6 +1238,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
12441238 func setControls( _ controls: Bool ) {
12451239 if _controls != controls || ( ( _playerLayer == nil ) && ( _playerViewController == nil ) ) {
12461240 _controls = controls
1241+ #if os(iOS)
1242+ if !isPictureInPictureActive( ) {
1243+ _pip? . deinitPipController ( )
1244+ }
1245+ #endif
12471246 if _controls {
12481247 DispatchQueue . main. async {
12491248 self . removePlayerLayer ( )
@@ -1828,15 +1827,38 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
18281827 }
18291828 }
18301829
1830+ private func findPlayerLayer( in view: UIView ) -> AVPlayerLayer ? {
1831+ if let layer = view. layer as? AVPlayerLayer {
1832+ return layer
1833+ }
1834+ for sublayer in view. layer. sublayers ?? [ ] {
1835+ if let playerLayer = sublayer as? AVPlayerLayer {
1836+ return playerLayer
1837+ }
1838+ }
1839+ for subview in view. subviews {
1840+ if let playerLayer = findPlayerLayer ( in: subview) {
1841+ return playerLayer
1842+ }
1843+ }
1844+ return nil
1845+ }
1846+
18311847 @objc
18321848 func enterPictureInPicture( ) {
1833- if _pip? . _pipController == nil {
1834- initPictureinPicture ( )
1835- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
1836- _playerViewController? . allowsPictureInPicturePlayback = true
1849+ #if os(iOS)
1850+ if _pip == nil {
1851+ initPictureinPicture ( )
18371852 }
1838- }
1839- _pip? . enterPictureInPicture ( )
1853+
1854+ if _pip? . _pipController == nil , let playerViewController = _playerViewController, _controls {
1855+ if let existingPlayerLayer = findPlayerLayer ( in: playerViewController. view) {
1856+ _pip? . setupPipController ( existingPlayerLayer)
1857+ }
1858+ }
1859+
1860+ _pip? . enterPictureInPicture ( )
1861+ #endif
18401862 }
18411863
18421864 @objc
@@ -1846,14 +1868,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
18461868 _pip? . exitPictureInPicture ( )
18471869 if _enterPictureInPictureOnLeave {
18481870 initPictureinPicture ( )
1849- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
1850- _playerViewController? . allowsPictureInPicturePlayback = true
1851- }
18521871 } else {
18531872 _pip? . deinitPipController ( )
1854- if #available( iOS 9 . 0 , tvOS 14 . 0 , * ) {
1855- _playerViewController? . allowsPictureInPicturePlayback = false
1856- }
18571873 }
18581874 }
18591875
0 commit comments