From 617b046789d7c5ece4335f33bc85e485c9862e5c Mon Sep 17 00:00:00 2001 From: Abdulrahman Alzenki Date: Fri, 26 Oct 2018 13:33:03 -0700 Subject: [PATCH 1/3] Implement picture in picture for iOS Test Plan: - Run on ipad - test out onIsPictureInPictureSupported, onIsPictureInPictureActive, restoreUserInterfaceForPictureInPictureStop, startPictureInPicture, stopPictureInPicture --- README.md | 73 +++++++++++++++++++++++++++++++++++++ Video.js | 28 ++++++++++++++ ios/Video/RCTVideo.h | 4 +- ios/Video/RCTVideo.m | 73 +++++++++++++++++++++++++++++++++++++ ios/Video/RCTVideoManager.m | 4 ++ 5 files changed, 181 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ee34399ce..3e28061fac 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,8 @@ var styles = StyleSheet.create({ * [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent) * [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss) * [onFullscreenPlayerDidDismiss](#onfullscreenplayerdiddismiss) +* [onIsPictureInPictureActive](#onispictureinpictureactive) +* [onIsPictureInPictureSupported](#onispictureinpicturesupported) * [onLoad](#onload) * [onLoadStart](#onloadstart) * [onProgress](#onprogress) @@ -308,7 +310,10 @@ var styles = StyleSheet.create({ * [dismissFullscreenPlayer](#dismissfullscreenplayer) * [presentFullscreenPlayer](#presentfullscreenplayer) * [save](#save) +* [restoreUserInterfaceForPictureInPictureStop](#restoreuserinterfaceforpictureinpicturestop) +* [startPictureInPicture](#startpictureinpicture) * [seek](#seek) +* [stopPictureInPicture](#stoppictureinpicture) ### Configurable props @@ -861,6 +866,38 @@ Payload: none Platforms: Android ExoPlayer, Android MediaPlayer, iOS +#### onIsPictureInPictureActive +Callback function that is called when picture in picture becames active on inactive. + +Property | Type | Description +--- | --- | --- +active | boolean | Boolean indicating whether picture in picture is active + +Example: +``` +{ + active: true +} +``` + +Platforms: iOS + +#### onIsPictureInPictureSupported +Callback function that is called initially to determine whether or not picture in picture is supported. + +Property | Type | Description +--- | --- | --- +supported | boolean | Boolean indicating whether picture in picture is supported + +Example: +``` +{ + supported: true +} +``` + +Platforms: iOS + #### onLoad Callback function that is called when the media is loaded and ready to play. @@ -1057,6 +1094,30 @@ Future: Platforms: iOS +#### restoreUserInterfaceForPictureInPictureStop +`restoreUserInterfaceForPictureInPictureStop(restore)` + +This function corresponds to Apple's [restoreUserInterfaceForPictureInPictureStop](https://developer.apple.com/documentation/avkit/avpictureinpicturecontrollerdelegate/1614703-pictureinpicturecontroller?language=objc). IMPORTANT: After picture in picture stops, this function must be called. + +Example: +``` +this.player.restoreUserInterfaceForPictureInPictureStop(true); +``` + +Platforms: iOS + +#### startPictureInPicture +`startPictureInPicture()` + +Calling this function will start picture in picture if it is supported. + +Example: +``` +this.player.startPictureInPicture(); +``` + +Platforms: iOS + #### seek() `seek(seconds)` @@ -1086,6 +1147,18 @@ this.player.seek(120, 50); // Seek to 2 minutes with +/- 50 milliseconds accurac Platforms: iOS +#### stopPictureInPicture +`stopPictureInPicture()` + +Calling this function will stop picture in picture if it is currently active. + +Example: +``` +this.player.stopPictureInPicture(); +``` + +Platforms: iOS + diff --git a/Video.js b/Video.js index 92544467c0..42e9731c93 100644 --- a/Video.js +++ b/Video.js @@ -78,6 +78,18 @@ export default class Video extends Component { return await NativeModules.VideoManager.save(options, findNodeHandle(this._root)); } + startPictureInPicture = () => { + this.setNativeProps({ pictureInPicture: true }); + }; + + stopPictureInPicture = () => { + this.setNativeProps({ pictureInPicture: false }); + }; + + restoreUserInterfaceForPictureInPictureStop = (restore) => { + this.setNativeProps({ restoreUserInterfaceForPIPStopCompletionHandler: restore }); + }; + _assignRoot = (component) => { this._root = component; }; @@ -198,6 +210,18 @@ export default class Video extends Component { } }; + _onIsPictureInPictureSupported = (event) => { + if (this.props.onIsPictureInPictureSupported) { + this.props.onIsPictureInPictureSupported(event.nativeEvent); + } + }; + + _onIsPictureInPictureActive = (event) => { + if (this.props.onIsPictureInPictureActive) { + this.props.onIsPictureInPictureActive(event.nativeEvent); + } + }; + _onAudioFocusChanged = (event) => { if (this.props.onAudioFocusChanged) { this.props.onAudioFocusChanged(event.nativeEvent); @@ -267,6 +291,8 @@ export default class Video extends Component { onPlaybackRateChange: this._onPlaybackRateChange, onAudioFocusChanged: this._onAudioFocusChanged, onAudioBecomingNoisy: this._onAudioBecomingNoisy, + onIsPictureInPictureSupported: this._onIsPictureInPictureSupported, + onIsPictureInPictureActive: this._onIsPictureInPictureActive, }); const posterStyle = { @@ -420,6 +446,8 @@ Video.propTypes = { onPlaybackRateChange: PropTypes.func, onAudioFocusChanged: PropTypes.func, onAudioBecomingNoisy: PropTypes.func, + onIsPictureInPictureSupported: PropTypes.func, + onIsPictureInPictureActive: PropTypes.func, onExternalPlaybackChange: PropTypes.func, /* Required by react-native */ diff --git a/ios/Video/RCTVideo.h b/ios/Video/RCTVideo.h index 05527a57fe..9c84bc157d 100644 --- a/ios/Video/RCTVideo.h +++ b/ios/Video/RCTVideo.h @@ -16,7 +16,7 @@ #if __has_include() @interface RCTVideo : UIView #else -@interface RCTVideo : UIView +@interface RCTVideo : UIView #endif @property (nonatomic, copy) RCTBubblingEventBlock onVideoLoadStart; @@ -38,6 +38,8 @@ @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange; @property (nonatomic, copy) RCTBubblingEventBlock onVideoExternalPlaybackChange; +@property (nonatomic, copy) RCTBubblingEventBlock onIsPictureInPictureSupported; +@property (nonatomic, copy) RCTBubblingEventBlock onIsPictureInPictureActive; - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; diff --git a/ios/Video/RCTVideo.m b/ios/Video/RCTVideo.m index 52b0342a6b..c95e59090b 100644 --- a/ios/Video/RCTVideo.m +++ b/ios/Video/RCTVideo.m @@ -27,6 +27,8 @@ @implementation RCTVideo AVPlayer *_player; AVPlayerItem *_playerItem; NSDictionary *_source; + AVPictureInPictureController *_pipController; + void (^__strong _Nonnull _restoreUserInterfaceForPIPStopCompletionHandler)(BOOL); BOOL _playerItemObserversSet; BOOL _playerBufferEmpty; AVPlayerLayer *_playerLayer; @@ -101,6 +103,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher _allowsExternalPlayback = YES; _playWhenInactive = false; _ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey + _restoreUserInterfaceForPIPStopCompletionHandler = NULL; #if __has_include() _videoCache = [RCTVideoCache sharedInstance]; #endif @@ -379,6 +382,12 @@ - (void)setSrc:(NSDictionary *)source @"target": self.reactTag }); } + + if (@available(iOS 9, *)) { + if (self.onIsPictureInPictureSupported) { + self.onIsPictureInPictureSupported(@{@"supported": [NSNumber numberWithBool:(bool)[AVPictureInPictureController isPictureInPictureSupported]]}); + } + } }]; }); _videoLoadStarted = YES; @@ -780,6 +789,37 @@ - (void)setPlayWhenInactive:(BOOL)playWhenInactive _playWhenInactive = playWhenInactive; } +- (void)setPictureInPicture:(BOOL)pictureInPicture +{ + if (_pipController && pictureInPicture && ![_pipController isPictureInPictureActive]) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_pipController startPictureInPicture]; + }); + } else if (_pipController && !pictureInPicture && [_pipController isPictureInPictureActive]) { + dispatch_async(dispatch_get_main_queue(), ^{ + [_pipController stopPictureInPicture]; + }); + } +} + +- (void)setRestoreUserInterfaceForPIPStopCompletionHandler:(BOOL)restore +{ + if (_restoreUserInterfaceForPIPStopCompletionHandler != NULL) { + _restoreUserInterfaceForPIPStopCompletionHandler(restore); + _restoreUserInterfaceForPIPStopCompletionHandler = NULL; + } +} + +- (void)setupPipController { + if (@available(iOS 9, *)) { + if (!_pipController && _playerLayer && [AVPictureInPictureController isPictureInPictureSupported]) { + // Create new controller passing reference to the AVPlayerLayer + _pipController = [[AVPictureInPictureController alloc] initWithPlayerLayer:_playerLayer]; + _pipController.delegate = self; + } + } +} + - (void)setIgnoreSilentSwitch:(NSString *)ignoreSilentSwitch { _ignoreSilentSwitch = ignoreSilentSwitch; @@ -1234,6 +1274,8 @@ - (void)usePlayerLayer [self.layer addSublayer:_playerLayer]; self.layer.needsDisplayOnBoundsChange = YES; + + [self setupPipController]; } } @@ -1490,4 +1532,35 @@ - (NSString *)cacheDirectoryPath { return array[0]; } +#pragma mark - Picture in Picture + +- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { + if (self.onIsPictureInPictureActive && _pipController) { + self.onIsPictureInPictureActive(@{@"active": [NSNumber numberWithBool:false]}); + } +} + +- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { + if (self.onIsPictureInPictureActive && _pipController) { + self.onIsPictureInPictureActive(@{@"active": [NSNumber numberWithBool:true]}); + } +} + +- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { + +} + +- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { + +} + +- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error { + +} + +- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL))completionHandler { + NSAssert(_restoreUserInterfaceForPIPStopCompletionHandler == NULL, @"restoreUserInterfaceForPIPStopCompletionHandler was not called after picture in picture was exited."); + _restoreUserInterfaceForPIPStopCompletionHandler = completionHandler; +} + @end diff --git a/ios/Video/RCTVideoManager.m b/ios/Video/RCTVideoManager.m index 1ca1b5b402..ebb6126b16 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -42,6 +42,8 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(filter, NSString); RCT_EXPORT_VIEW_PROPERTY(filterEnabled, BOOL); RCT_EXPORT_VIEW_PROPERTY(progressUpdateInterval, float); +RCT_EXPORT_VIEW_PROPERTY(pictureInPicture, BOOL); +RCT_EXPORT_VIEW_PROPERTY(restoreUserInterfaceForPIPStopCompletionHandler, BOOL); /* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */ RCT_EXPORT_VIEW_PROPERTY(onVideoLoadStart, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoLoad, RCTBubblingEventBlock); @@ -77,6 +79,8 @@ - (dispatch_queue_t)methodQueue } }]; } +RCT_EXPORT_VIEW_PROPERTY(onIsPictureInPictureSupported, RCTBubblingEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onIsPictureInPictureActive, RCTBubblingEventBlock); - (NSDictionary *)constantsToExport { From 4a1615119554a607c48aabd8d91353a4dda83534 Mon Sep 17 00:00:00 2001 From: Abdulrahman AlZanki Date: Wed, 14 Nov 2018 10:23:56 -0800 Subject: [PATCH 2/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e28061fac..9d9e24f266 100644 --- a/README.md +++ b/README.md @@ -867,7 +867,7 @@ Payload: none Platforms: Android ExoPlayer, Android MediaPlayer, iOS #### onIsPictureInPictureActive -Callback function that is called when picture in picture becames active on inactive. +Callback function that is called when picture in picture becomes active or inactive. Property | Type | Description --- | --- | --- From 62dc913cb36f528ccb004cd24071b3299f233b4a Mon Sep 17 00:00:00 2001 From: Abdulrahman Alzenki Date: Mon, 26 Nov 2018 14:23:04 -0800 Subject: [PATCH 3/3] Address some of the feedback from the pull reqeust --- README.md | 101 +++++++++++++----------------------- Video.js | 33 +++++------- ios/Video/RCTVideo.h | 4 +- ios/Video/RCTVideo.m | 42 ++++++++------- ios/Video/RCTVideoManager.m | 6 +-- 5 files changed, 79 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 9d9e24f266..dc90f226a8 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,7 @@ var styles = StyleSheet.create({ * [maxBitRate](#maxbitrate) * [muted](#muted) * [paused](#paused) +* [pictureInPicture](#pictureinpicture) * [playInBackground](#playinbackground) * [playWhenInactive](#playwheninactive) * [poster](#poster) @@ -298,12 +299,12 @@ var styles = StyleSheet.create({ * [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent) * [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss) * [onFullscreenPlayerDidDismiss](#onfullscreenplayerdiddismiss) -* [onIsPictureInPictureActive](#onispictureinpictureactive) -* [onIsPictureInPictureSupported](#onispictureinpicturesupported) * [onLoad](#onload) * [onLoadStart](#onloadstart) +* [onPictureInPictureStatusChanged](#onpictureinpicturestatuschanged) * [onProgress](#onprogress) * [onSeek](#onseek) +* [onRestoreUserInterfaceForPictureInPictureStop](#onrestoreuserinterfaceforpictureinpicturestop) * [onTimedMetadata](#ontimedmetadata) ### Methods @@ -311,9 +312,7 @@ var styles = StyleSheet.create({ * [presentFullscreenPlayer](#presentfullscreenplayer) * [save](#save) * [restoreUserInterfaceForPictureInPictureStop](#restoreuserinterfaceforpictureinpicturestop) -* [startPictureInPicture](#startpictureinpicture) * [seek](#seek) -* [stopPictureInPicture](#stoppictureinpicture) ### Configurable props @@ -491,6 +490,13 @@ Controls whether the media is paused Platforms: all +#### pictureInPicture +Determine whether the media should played as picture in picture. +* **false (default)** - Don't not play as picture in picture +* **true** - Play the media as picture in picture + +Platforms: iOS + #### playInBackground Determine whether the media should continue playing while the app is in the background. This allows customers to continue listening to the audio. * **false (default)** - Don't continue playing the media @@ -866,38 +872,6 @@ Payload: none Platforms: Android ExoPlayer, Android MediaPlayer, iOS -#### onIsPictureInPictureActive -Callback function that is called when picture in picture becomes active or inactive. - -Property | Type | Description ---- | --- | --- -active | boolean | Boolean indicating whether picture in picture is active - -Example: -``` -{ - active: true -} -``` - -Platforms: iOS - -#### onIsPictureInPictureSupported -Callback function that is called initially to determine whether or not picture in picture is supported. - -Property | Type | Description ---- | --- | --- -supported | boolean | Boolean indicating whether picture in picture is supported - -Example: -``` -{ - supported: true -} -``` - -Platforms: iOS - #### onLoad Callback function that is called when the media is loaded and ready to play. @@ -963,6 +937,22 @@ Example: Platforms: all +#### onPictureInPictureStatusChanged +Callback function that is called when picture in picture becomes active or inactive. + +Property | Type | Description +--- | --- | --- +isActive | boolean | Boolean indicating whether picture in picture is active + +Example: +``` +{ +isActive: true +} +``` + +Platforms: iOS + #### onProgress Callback function that is called every progressUpdateInterval seconds with info about which position the media is currently playing. @@ -1006,6 +996,13 @@ Both the currentTime & seekTime are reported because the video player may not se Platforms: Android ExoPlayer, Android MediaPlayer, iOS, Windows UWP +#### onRestoreUserInterfaceForPictureInPictureStop +Callback function that corresponds to Apple's [`restoreUserInterfaceForPictureInPictureStopWithCompletionHandler`](https://developer.apple.com/documentation/avkit/avpictureinpicturecontrollerdelegate/1614703-pictureinpicturecontroller?language=objc). Call `restoreUserInterfaceForPictureInPictureStopCompleted` inside of this function when done restoring the user interface. + +Payload: none + +Platforms: iOS + #### onTimedMetadata Callback function that is called when timed metadata becomes available @@ -1094,26 +1091,14 @@ Future: Platforms: iOS -#### restoreUserInterfaceForPictureInPictureStop -`restoreUserInterfaceForPictureInPictureStop(restore)` +#### restoreUserInterfaceForPictureInPictureStopCompleted +`restoreUserInterfaceForPictureInPictureStopCompleted(restored)` -This function corresponds to Apple's [restoreUserInterfaceForPictureInPictureStop](https://developer.apple.com/documentation/avkit/avpictureinpicturecontrollerdelegate/1614703-pictureinpicturecontroller?language=objc). IMPORTANT: After picture in picture stops, this function must be called. +This function corresponds to the completion handler in Apple's [restoreUserInterfaceForPictureInPictureStop](https://developer.apple.com/documentation/avkit/avpictureinpicturecontrollerdelegate/1614703-pictureinpicturecontroller?language=objc). IMPORTANT: This function must be called after `onRestoreUserInterfaceForPictureInPictureStop` is called. Example: ``` -this.player.restoreUserInterfaceForPictureInPictureStop(true); -``` - -Platforms: iOS - -#### startPictureInPicture -`startPictureInPicture()` - -Calling this function will start picture in picture if it is supported. - -Example: -``` -this.player.startPictureInPicture(); +this.player.restoreUserInterfaceForPictureInPictureStopCompleted(true); ``` Platforms: iOS @@ -1147,18 +1132,6 @@ this.player.seek(120, 50); // Seek to 2 minutes with +/- 50 milliseconds accurac Platforms: iOS -#### stopPictureInPicture -`stopPictureInPicture()` - -Calling this function will stop picture in picture if it is currently active. - -Example: -``` -this.player.stopPictureInPicture(); -``` - -Platforms: iOS - diff --git a/Video.js b/Video.js index 42e9731c93..f33d102fba 100644 --- a/Video.js +++ b/Video.js @@ -78,16 +78,8 @@ export default class Video extends Component { return await NativeModules.VideoManager.save(options, findNodeHandle(this._root)); } - startPictureInPicture = () => { - this.setNativeProps({ pictureInPicture: true }); - }; - - stopPictureInPicture = () => { - this.setNativeProps({ pictureInPicture: false }); - }; - - restoreUserInterfaceForPictureInPictureStop = (restore) => { - this.setNativeProps({ restoreUserInterfaceForPIPStopCompletionHandler: restore }); + restoreUserInterfaceForPictureInPictureStopCompleted = (restored) => { + this.setNativeProps({ restoreUserInterfaceForPIPStopCompletionHandler: restored }); }; _assignRoot = (component) => { @@ -210,15 +202,15 @@ export default class Video extends Component { } }; - _onIsPictureInPictureSupported = (event) => { - if (this.props.onIsPictureInPictureSupported) { - this.props.onIsPictureInPictureSupported(event.nativeEvent); + _onPictureInPictureStatusChanged = (event) => { + if (this.props.onPictureInPictureStatusChanged) { + this.props.onPictureInPictureStatusChanged(event.nativeEvent); } }; - _onIsPictureInPictureActive = (event) => { - if (this.props.onIsPictureInPictureActive) { - this.props.onIsPictureInPictureActive(event.nativeEvent); + _onRestoreUserInterfaceForPictureInPictureStop = (event) => { + if (this.props.onRestoreUserInterfaceForPictureInPictureStop) { + this.props.onRestoreUserInterfaceForPictureInPictureStop(); } }; @@ -291,8 +283,8 @@ export default class Video extends Component { onPlaybackRateChange: this._onPlaybackRateChange, onAudioFocusChanged: this._onAudioFocusChanged, onAudioBecomingNoisy: this._onAudioBecomingNoisy, - onIsPictureInPictureSupported: this._onIsPictureInPictureSupported, - onIsPictureInPictureActive: this._onIsPictureInPictureActive, + onPictureInPictureStatusChanged: this._onPictureInPictureStatusChanged, + onRestoreUserInterfaceForPictureInPictureStop: this._onRestoreUserInterfaceForPictureInPictureStop, }); const posterStyle = { @@ -415,6 +407,7 @@ Video.propTypes = { }), stereoPan: PropTypes.number, rate: PropTypes.number, + pictureInPicture: PropTypes.bool, playInBackground: PropTypes.bool, playWhenInactive: PropTypes.bool, ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']), @@ -446,8 +439,8 @@ Video.propTypes = { onPlaybackRateChange: PropTypes.func, onAudioFocusChanged: PropTypes.func, onAudioBecomingNoisy: PropTypes.func, - onIsPictureInPictureSupported: PropTypes.func, - onIsPictureInPictureActive: PropTypes.func, + onPictureInPictureStatusChanged: PropTypes.func, + needsToRestoreUserInterfaceForPictureInPictureStop: PropTypes.func, onExternalPlaybackChange: PropTypes.func, /* Required by react-native */ diff --git a/ios/Video/RCTVideo.h b/ios/Video/RCTVideo.h index 9c84bc157d..ad98db7ffa 100644 --- a/ios/Video/RCTVideo.h +++ b/ios/Video/RCTVideo.h @@ -38,8 +38,8 @@ @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange; @property (nonatomic, copy) RCTBubblingEventBlock onVideoExternalPlaybackChange; -@property (nonatomic, copy) RCTBubblingEventBlock onIsPictureInPictureSupported; -@property (nonatomic, copy) RCTBubblingEventBlock onIsPictureInPictureActive; +@property (nonatomic, copy) RCTBubblingEventBlock onPictureInPictureStatusChanged; +@property (nonatomic, copy) RCTBubblingEventBlock onRestoreUserInterfaceForPictureInPictureStop; - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; diff --git a/ios/Video/RCTVideo.m b/ios/Video/RCTVideo.m index c95e59090b..60a42bc9b7 100644 --- a/ios/Video/RCTVideo.m +++ b/ios/Video/RCTVideo.m @@ -66,6 +66,7 @@ @implementation RCTVideo BOOL _playbackStalled; BOOL _playInBackground; BOOL _playWhenInactive; + BOOL _pictureInPicture; NSString * _ignoreSilentSwitch; NSString * _resizeMode; BOOL _fullscreen; @@ -102,6 +103,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher _playInBackground = false; _allowsExternalPlayback = YES; _playWhenInactive = false; + _pictureInPicture = false; _ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey _restoreUserInterfaceForPIPStopCompletionHandler = NULL; #if __has_include() @@ -382,12 +384,6 @@ - (void)setSrc:(NSDictionary *)source @"target": self.reactTag }); } - - if (@available(iOS 9, *)) { - if (self.onIsPictureInPictureSupported) { - self.onIsPictureInPictureSupported(@{@"supported": [NSNumber numberWithBool:(bool)[AVPictureInPictureController isPictureInPictureSupported]]}); - } - } }]; }); _videoLoadStarted = YES; @@ -791,11 +787,16 @@ - (void)setPlayWhenInactive:(BOOL)playWhenInactive - (void)setPictureInPicture:(BOOL)pictureInPicture { - if (_pipController && pictureInPicture && ![_pipController isPictureInPictureActive]) { + if (_pictureInPicture == pictureInPicture) { + return; + } + + _pictureInPicture = pictureInPicture; + if (_pipController && _pictureInPicture && ![_pipController isPictureInPictureActive]) { dispatch_async(dispatch_get_main_queue(), ^{ [_pipController startPictureInPicture]; }); - } else if (_pipController && !pictureInPicture && [_pipController isPictureInPictureActive]) { + } else if (_pipController && !_pictureInPicture && [_pipController isPictureInPictureActive]) { dispatch_async(dispatch_get_main_queue(), ^{ [_pipController stopPictureInPicture]; }); @@ -811,12 +812,10 @@ - (void)setRestoreUserInterfaceForPIPStopCompletionHandler:(BOOL)restore } - (void)setupPipController { - if (@available(iOS 9, *)) { - if (!_pipController && _playerLayer && [AVPictureInPictureController isPictureInPictureSupported]) { - // Create new controller passing reference to the AVPlayerLayer - _pipController = [[AVPictureInPictureController alloc] initWithPlayerLayer:_playerLayer]; - _pipController.delegate = self; - } + if (!_pipController && _playerLayer && [AVPictureInPictureController isPictureInPictureSupported]) { + // Create new controller passing reference to the AVPlayerLayer + _pipController = [[AVPictureInPictureController alloc] initWithPlayerLayer:_playerLayer]; + _pipController.delegate = self; } } @@ -1535,14 +1534,18 @@ - (NSString *)cacheDirectoryPath { #pragma mark - Picture in Picture - (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { - if (self.onIsPictureInPictureActive && _pipController) { - self.onIsPictureInPictureActive(@{@"active": [NSNumber numberWithBool:false]}); + if (self.onPictureInPictureStatusChanged) { + self.onPictureInPictureStatusChanged(@{ + @"isActive": [NSNumber numberWithBool:false] + }); } } - (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { - if (self.onIsPictureInPictureActive && _pipController) { - self.onIsPictureInPictureActive(@{@"active": [NSNumber numberWithBool:true]}); + if (self.onPictureInPictureStatusChanged) { + self.onPictureInPictureStatusChanged(@{ + @"isActive": [NSNumber numberWithBool:true] + }); } } @@ -1560,6 +1563,9 @@ - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPict - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL))completionHandler { NSAssert(_restoreUserInterfaceForPIPStopCompletionHandler == NULL, @"restoreUserInterfaceForPIPStopCompletionHandler was not called after picture in picture was exited."); + if (self.onRestoreUserInterfaceForPictureInPictureStop) { + self.onRestoreUserInterfaceForPictureInPictureStop(@{}); + } _restoreUserInterfaceForPIPStopCompletionHandler = completionHandler; } diff --git a/ios/Video/RCTVideoManager.m b/ios/Video/RCTVideoManager.m index ebb6126b16..a608f32e6b 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -32,6 +32,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(volume, float); RCT_EXPORT_VIEW_PROPERTY(playInBackground, BOOL); RCT_EXPORT_VIEW_PROPERTY(playWhenInactive, BOOL); +RCT_EXPORT_VIEW_PROPERTY(pictureInPicture, BOOL); RCT_EXPORT_VIEW_PROPERTY(ignoreSilentSwitch, NSString); RCT_EXPORT_VIEW_PROPERTY(rate, float); RCT_EXPORT_VIEW_PROPERTY(seek, NSDictionary); @@ -42,7 +43,6 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(filter, NSString); RCT_EXPORT_VIEW_PROPERTY(filterEnabled, BOOL); RCT_EXPORT_VIEW_PROPERTY(progressUpdateInterval, float); -RCT_EXPORT_VIEW_PROPERTY(pictureInPicture, BOOL); RCT_EXPORT_VIEW_PROPERTY(restoreUserInterfaceForPIPStopCompletionHandler, BOOL); /* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */ RCT_EXPORT_VIEW_PROPERTY(onVideoLoadStart, RCTBubblingEventBlock); @@ -79,8 +79,8 @@ - (dispatch_queue_t)methodQueue } }]; } -RCT_EXPORT_VIEW_PROPERTY(onIsPictureInPictureSupported, RCTBubblingEventBlock); -RCT_EXPORT_VIEW_PROPERTY(onIsPictureInPictureActive, RCTBubblingEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onPictureInPictureStatusChanged, RCTBubblingEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onRestoreUserInterfaceForPictureInPictureStop, RCTBubblingEventBlock); - (NSDictionary *)constantsToExport {