Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introducing onExternalPlaybackChange #1235

Merged
merged 4 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ var styles = StyleSheet.create({
### Event props
* [onAudioBecomingNoisy](#onaudiobecomingnoisy)
* [onEnd](#onend)
* [onExternalPlaybackChange](#onexternalplaybackchange)
* [onFullscreenPlayerWillPresent](#onfullscreenplayerwillpresent)
* [onFullscreenPlayerDidPresent](#onfullscreenplayerdidpresent)
* [onFullscreenPlayerWillDismiss](#onfullscreenplayerwilldismiss)
Expand Down Expand Up @@ -644,6 +645,24 @@ Payload: none

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
Callback function that is called when the player is about to enter fullscreen mode.

Expand Down
8 changes: 8 additions & 0 deletions Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export default class Video extends Component {
this.props.onPlaybackRateChange(event.nativeEvent);
}
};

_onExternalPlaybackChange = (event) => {
if (this.props.onExternalPlaybackChange) {
this.props.onExternalPlaybackChange(event.nativeEvent);
}
}

_onAudioBecomingNoisy = () => {
if (this.props.onAudioBecomingNoisy) {
Expand Down Expand Up @@ -246,6 +252,7 @@ export default class Video extends Component {
onPlaybackRateChange: this._onPlaybackRateChange,
onAudioFocusChanged: this._onAudioFocusChanged,
onAudioBecomingNoisy: this._onAudioBecomingNoisy,
onExternalPlaybackChange: this._onExternalPlaybackChange,
});

const posterStyle = {
Expand Down Expand Up @@ -365,6 +372,7 @@ Video.propTypes = {
onPlaybackRateChange: PropTypes.func,
onAudioFocusChanged: PropTypes.func,
onAudioBecomingNoisy: PropTypes.func,
onExternalPlaybackChange: PropTypes.func,

/* Required by react-native */
scaleX: PropTypes.number,
Expand Down
1 change: 1 addition & 0 deletions ios/Video/RCTVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackStalled;
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume;
@property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange;
@property (nonatomic, copy) RCTBubblingEventBlock onExternalPlaybackChange;

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;

Expand Down
20 changes: 20 additions & 0 deletions ios/Video/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
static NSString *const readyForDisplayKeyPath = @"readyForDisplay";
static NSString *const playbackRate = @"rate";
static NSString *const timedMetadata = @"timedMetadata";
static NSString *const externalPlaybackActive = @"externalPlaybackActive";

static int const RCTVideoUnset = -1;

Expand All @@ -35,6 +36,7 @@ @implementation RCTVideo
/* Required to publish events */
RCTEventDispatcher *_eventDispatcher;
BOOL _playbackRateObserverRegistered;
BOOL _isExternalPlaybackActiveObserverRegistered;
BOOL _videoLoadStarted;

bool _pendingSeek;
Expand Down Expand Up @@ -74,6 +76,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
_eventDispatcher = eventDispatcher;

_playbackRateObserverRegistered = NO;
_isExternalPlaybackActiveObserverRegistered = NO;
_playbackStalled = NO;
_rate = 1.0;
_volume = 1.0;
Expand Down Expand Up @@ -333,12 +336,19 @@ - (void)setSrc:(NSDictionary *)source
[_player removeObserver:self forKeyPath:playbackRate context:nil];
_playbackRateObserverRegistered = NO;
}
if (_isExternalPlaybackActiveObserverRegistered) {
[_player removeObserver:self forKeyPath:externalPlaybackActive context:nil];
_isExternalPlaybackActiveObserverRegistered = NO;
}

_player = [AVPlayer playerWithPlayerItem:_playerItem];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[_player addObserver:self forKeyPath:playbackRate options:0 context:nil];
_playbackRateObserverRegistered = YES;

[_player addObserver:self forKeyPath:externalPlaybackActive options:0 context:nil];
_isExternalPlaybackActiveObserverRegistered = YES;

[self addPlayerTimeObserver];

Expand Down Expand Up @@ -646,6 +656,12 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
_playbackStalled = NO;
}
}
else if([keyPath isEqualToString:externalPlaybackActive]) {
if(self.onExternalPlaybackChange) {
self.onExternalPlaybackChange(@{@"isExternalPlaybackActive": [NSNumber numberWithBool:_player.isExternalPlaybackActive],
@"target": self.reactTag});
}
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
Expand Down Expand Up @@ -1290,6 +1306,10 @@ - (void)removeFromSuperview
[_player removeObserver:self forKeyPath:playbackRate context:nil];
_playbackRateObserverRegistered = NO;
}
if (_isExternalPlaybackActiveObserverRegistered) {
[_player removeObserver:self forKeyPath:externalPlaybackActive context:nil];
_isExternalPlaybackActiveObserverRegistered = NO;
}
_player = nil;

[self removePlayerLayer];
Expand Down
1 change: 1 addition & 0 deletions ios/Video/RCTVideoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_VIEW_PROPERTY(onPlaybackStalled, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackResume, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onPlaybackRateChange, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onExternalPlaybackChange, RCTBubblingEventBlock);

- (NSDictionary *)constantsToExport
{
Expand Down