Skip to content

Commit

Permalink
Address some of the feedback from the pull reqeust
Browse files Browse the repository at this point in the history
  • Loading branch information
aalzanki committed Nov 27, 2018
1 parent 78b8ca2 commit fe04674
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 88 deletions.
69 changes: 21 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ var styles = StyleSheet.create({
* [ignoreSilentSwitch](#ignoresilentswitch)
* [muted](#muted)
* [paused](#paused)
* [pictureInPicture](#pictureinpicture)
* [playInBackground](#playinbackground)
* [playWhenInactive](#playwheninactive)
* [poster](#poster)
Expand All @@ -283,15 +284,15 @@ var styles = StyleSheet.create({
* [volume](#volume)

### Event props
* [needsToRestoreUserInterfaceForPictureInPictureStop](#needstorestoreuserinterfaceforpictureinpicturestop)
* [onAudioBecomingNoisy](#onaudiobecomingnoisy)
* [onEnd](#onend)
* [onExternalPlaybackChange](#onexternalplaybackchange)
* [onFullscreenPlayerWillPresent](#onfullscreenplayerwillpresent)
* [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent)
* [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss)
* [onFullscreenPlayerDidDismiss](#onfullscreenplayerdiddismiss)
* [onIsPictureInPictureActive](#onispictureinpictureactive)
* [onIsPictureInPictureSupported](#onispictureinpicturesupported)
* [onPictureInPictureStatusChanged](#onpictureinpicturestatuschanged)
* [onLoad](#onload)
* [onLoadStart](#onloadstart)
* [onProgress](#onprogress)
Expand All @@ -301,9 +302,7 @@ var styles = StyleSheet.create({
* [dismissFullscreenPlayer](#dismissfullscreenplayer)
* [presentFullscreenPlayer](#presentfullscreenplayer)
* [restoreUserInterfaceForPictureInPictureStop](#restoreuserinterfaceforpictureinpicturestop)
* [startPictureInPicture](#startpictureinpicture)
* [seek](#seek)
* [stopPictureInPicture](#stoppictureinpicture)

### Configurable props

Expand Down Expand Up @@ -418,6 +417,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
Expand Down Expand Up @@ -672,6 +678,13 @@ Platforms: all

### Event props

#### needsToRestoreUserInterfaceForPictureInPictureStop
Callback function that is called when picture in picture is stopped and requires restoring the user interface. Call `restoreUserInterfaceForPictureInPictureStop` when this method is called.

Payload: none

Platforms: iOS

#### onAudioBecomingNoisy
Callback function that is called when the audio is about to become 'noisy' due to a change in audio outputs. Typically this is called when audio output is being switched from an external source like headphones back to the internal speaker. It's a good idea to pause the media when this happens so the speaker doesn't start blasting sound.

Expand Down Expand Up @@ -732,33 +745,17 @@ Payload: none

Platforms: Android ExoPlayer, Android MediaPlayer, iOS

#### onIsPictureInPictureActive
#### onPictureInPictureStatusChanged
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
isActive | boolean | Boolean indicating whether picture in picture is active

Example:
```
{
supported: true
isActive: true
}
```

Expand Down Expand Up @@ -913,7 +910,7 @@ Platforms: Android ExoPlayer, Android MediaPlayer, 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.
This function corresponds to Apple's [restoreUserInterfaceForPictureInPictureStop](https://developer.apple.com/documentation/avkit/avpictureinpicturecontrollerdelegate/1614703-pictureinpicturecontroller?language=objc). IMPORTANT: This function must be called after needsToRestoreUserInterfaceForPictureInPictureStop is called.

Example:
```
Expand All @@ -922,18 +919,6 @@ 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)`

Expand Down Expand Up @@ -963,18 +948,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


### iOS App Transport Security

Expand Down
29 changes: 11 additions & 18 deletions Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ export default class Video extends Component {
this.setNativeProps({ fullscreen: false });
};

startPictureInPicture = () => {
this.setNativeProps({ pictureInPicture: true });
};

stopPictureInPicture = () => {
this.setNativeProps({ pictureInPicture: false });
};

restoreUserInterfaceForPictureInPictureStop = (restore) => {
this.setNativeProps({ restoreUserInterfaceForPIPStopCompletionHandler: restore });
};
Expand Down Expand Up @@ -197,15 +189,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();
}
};

Expand Down Expand Up @@ -277,8 +269,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 = {
Expand Down Expand Up @@ -373,6 +365,7 @@ Video.propTypes = {
}),
stereoPan: PropTypes.number,
rate: PropTypes.number,
pictureInPicture: PropTypes.bool,
playInBackground: PropTypes.bool,
playWhenInactive: PropTypes.bool,
ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']),
Expand Down Expand Up @@ -400,8 +393,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 */
Expand Down
3 changes: 2 additions & 1 deletion ios/Video/RCTVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
@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;

Expand Down
42 changes: 24 additions & 18 deletions ios/Video/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ @implementation RCTVideo
BOOL _playbackStalled;
BOOL _playInBackground;
BOOL _playWhenInactive;
BOOL _pictureInPicture;
NSString * _ignoreSilentSwitch;
NSString * _resizeMode;
BOOL _fullscreen;
Expand Down Expand Up @@ -95,6 +96,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
_playInBackground = false;
_allowsExternalPlayback = YES;
_playWhenInactive = false;
_pictureInPicture = false;
_ignoreSilentSwitch = @"inherit"; // inherit, ignore, obey
_restoreUserInterfaceForPIPStopCompletionHandler = NULL;
#if __has_include(<react-native-video/RCTVideoCache.h>)
Expand Down Expand Up @@ -372,12 +374,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;
Expand Down Expand Up @@ -758,11 +754,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];
});
Expand All @@ -778,12 +779,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;
}
}

Expand Down Expand Up @@ -1383,14 +1382,18 @@ - (void)removeFromSuperview
#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]
});
}
}

Expand All @@ -1408,6 +1411,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;
}

Expand Down
6 changes: 3 additions & 3 deletions ios/Video/RCTVideoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ - (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);
RCT_EXPORT_VIEW_PROPERTY(currentTime, float);
RCT_EXPORT_VIEW_PROPERTY(fullscreen, BOOL);
RCT_EXPORT_VIEW_PROPERTY(fullscreenOrientation, NSString);
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);
Expand All @@ -60,8 +60,8 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_VIEW_PROPERTY(onPlaybackResume, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackRateChange, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onVideoExternalPlaybackChange, RCTBubblingEventBlock);
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
{
Expand Down

0 comments on commit fe04674

Please sign in to comment.