Merge pull request #1235 from arturjaworski/master

introducing onExternalPlaybackChange
This commit is contained in:
Hampton Maxwell 2018-10-07 15:58:40 -07:00 committed by GitHub
commit 2688dd9e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 0 deletions

View File

@ -276,6 +276,7 @@ var styles = StyleSheet.create({
### Event props ### Event props
* [onAudioBecomingNoisy](#onaudiobecomingnoisy) * [onAudioBecomingNoisy](#onaudiobecomingnoisy)
* [onEnd](#onend) * [onEnd](#onend)
* [onExternalPlaybackChange](#onexternalplaybackchange)
* [onFullscreenPlayerWillPresent](#onfullscreenplayerwillpresent) * [onFullscreenPlayerWillPresent](#onfullscreenplayerwillpresent)
* [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent) * [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent)
* [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss) * [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss)
@ -644,6 +645,24 @@ Payload: none
Platforms: all Platforms: all
#### onExternalPlaybackChange
Callback function that is called when external playback mode for current playing video has changed. Mostly useful when connecting/disconnecting to Apple TV it's called on connection/disconnection.
Payload:
Property | Type | Description
--- | --- | ---
isExternalPlaybackActive | boolean | Boolean indicating whether external playback mode is active
Example:
```
{
isExternalPlaybackActive: true
}
```
Platforms: iOS
#### onFullscreenPlayerWillPresent #### onFullscreenPlayerWillPresent
Callback function that is called when the player is about to enter fullscreen mode. Callback function that is called when the player is about to enter fullscreen mode.

View File

@ -173,6 +173,12 @@ export default class Video extends Component {
} }
}; };
_onExternalPlaybackChange = (event) => {
if (this.props.onExternalPlaybackChange) {
this.props.onExternalPlaybackChange(event.nativeEvent);
}
}
_onAudioBecomingNoisy = () => { _onAudioBecomingNoisy = () => {
if (this.props.onAudioBecomingNoisy) { if (this.props.onAudioBecomingNoisy) {
this.props.onAudioBecomingNoisy(); this.props.onAudioBecomingNoisy();
@ -246,6 +252,7 @@ export default class Video extends Component {
onPlaybackRateChange: this._onPlaybackRateChange, onPlaybackRateChange: this._onPlaybackRateChange,
onAudioFocusChanged: this._onAudioFocusChanged, onAudioFocusChanged: this._onAudioFocusChanged,
onAudioBecomingNoisy: this._onAudioBecomingNoisy, onAudioBecomingNoisy: this._onAudioBecomingNoisy,
onExternalPlaybackChange: this._onExternalPlaybackChange,
}); });
const posterStyle = { const posterStyle = {
@ -365,6 +372,7 @@ Video.propTypes = {
onPlaybackRateChange: PropTypes.func, onPlaybackRateChange: PropTypes.func,
onAudioFocusChanged: PropTypes.func, onAudioFocusChanged: PropTypes.func,
onAudioBecomingNoisy: PropTypes.func, onAudioBecomingNoisy: PropTypes.func,
onExternalPlaybackChange: PropTypes.func,
/* Required by react-native */ /* Required by react-native */
scaleX: PropTypes.number, scaleX: PropTypes.number,

View File

@ -35,6 +35,7 @@
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackStalled; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackStalled;
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume;
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange;
@property (nonatomic, copy) RCTBubblingEventBlock onExternalPlaybackChange;
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

View File

@ -12,6 +12,7 @@ static NSString *const playbackBufferEmptyKeyPath = @"playbackBufferEmpty";
static NSString *const readyForDisplayKeyPath = @"readyForDisplay"; static NSString *const readyForDisplayKeyPath = @"readyForDisplay";
static NSString *const playbackRate = @"rate"; static NSString *const playbackRate = @"rate";
static NSString *const timedMetadata = @"timedMetadata"; static NSString *const timedMetadata = @"timedMetadata";
static NSString *const externalPlaybackActive = @"externalPlaybackActive";
static int const RCTVideoUnset = -1; static int const RCTVideoUnset = -1;
@ -35,6 +36,7 @@ static int const RCTVideoUnset = -1;
/* Required to publish events */ /* Required to publish events */
RCTEventDispatcher *_eventDispatcher; RCTEventDispatcher *_eventDispatcher;
BOOL _playbackRateObserverRegistered; BOOL _playbackRateObserverRegistered;
BOOL _isExternalPlaybackActiveObserverRegistered;
BOOL _videoLoadStarted; BOOL _videoLoadStarted;
bool _pendingSeek; bool _pendingSeek;
@ -74,6 +76,7 @@ static int const RCTVideoUnset = -1;
_eventDispatcher = eventDispatcher; _eventDispatcher = eventDispatcher;
_playbackRateObserverRegistered = NO; _playbackRateObserverRegistered = NO;
_isExternalPlaybackActiveObserverRegistered = NO;
_playbackStalled = NO; _playbackStalled = NO;
_rate = 1.0; _rate = 1.0;
_volume = 1.0; _volume = 1.0;
@ -333,6 +336,10 @@ static int const RCTVideoUnset = -1;
[_player removeObserver:self forKeyPath:playbackRate context:nil]; [_player removeObserver:self forKeyPath:playbackRate context:nil];
_playbackRateObserverRegistered = NO; _playbackRateObserverRegistered = NO;
} }
if (_isExternalPlaybackActiveObserverRegistered) {
[_player removeObserver:self forKeyPath:externalPlaybackActive context:nil];
_isExternalPlaybackActiveObserverRegistered = NO;
}
_player = [AVPlayer playerWithPlayerItem:_playerItem]; _player = [AVPlayer playerWithPlayerItem:_playerItem];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone; _player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
@ -340,6 +347,9 @@ static int const RCTVideoUnset = -1;
[_player addObserver:self forKeyPath:playbackRate options:0 context:nil]; [_player addObserver:self forKeyPath:playbackRate options:0 context:nil];
_playbackRateObserverRegistered = YES; _playbackRateObserverRegistered = YES;
[_player addObserver:self forKeyPath:externalPlaybackActive options:0 context:nil];
_isExternalPlaybackActiveObserverRegistered = YES;
[self addPlayerTimeObserver]; [self addPlayerTimeObserver];
//Perform on next run loop, otherwise onVideoLoadStart is nil //Perform on next run loop, otherwise onVideoLoadStart is nil
@ -646,6 +656,12 @@ static int const RCTVideoUnset = -1;
_playbackStalled = NO; _playbackStalled = NO;
} }
} }
else if([keyPath isEqualToString:externalPlaybackActive]) {
if(self.onExternalPlaybackChange) {
self.onExternalPlaybackChange(@{@"isExternalPlaybackActive": [NSNumber numberWithBool:_player.isExternalPlaybackActive],
@"target": self.reactTag});
}
}
} else { } else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
} }
@ -1290,6 +1306,10 @@ static int const RCTVideoUnset = -1;
[_player removeObserver:self forKeyPath:playbackRate context:nil]; [_player removeObserver:self forKeyPath:playbackRate context:nil];
_playbackRateObserverRegistered = NO; _playbackRateObserverRegistered = NO;
} }
if (_isExternalPlaybackActiveObserverRegistered) {
[_player removeObserver:self forKeyPath:externalPlaybackActive context:nil];
_isExternalPlaybackActiveObserverRegistered = NO;
}
_player = nil; _player = nil;
[self removePlayerLayer]; [self removePlayerLayer];

View File

@ -56,6 +56,7 @@ RCT_EXPORT_VIEW_PROPERTY(onReadyForDisplay, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackStalled, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackStalled, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackResume, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackResume, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackRateChange, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackRateChange, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onExternalPlaybackChange, RCTBubblingEventBlock);
- (NSDictionary *)constantsToExport - (NSDictionary *)constantsToExport
{ {