Protect events (#444)

This commit is contained in:
Olof Dahlbom 2017-01-16 17:27:08 +01:00 committed by Matt Apperson
parent 9a936c9e8f
commit 2b709a5d54

View File

@ -184,7 +184,7 @@ static NSString *const playbackRate = @"rate";
CMTime currentTime = _player.currentTime; CMTime currentTime = _player.currentTime;
const Float64 duration = CMTimeGetSeconds(playerDuration); const Float64 duration = CMTimeGetSeconds(playerDuration);
const Float64 currentTimeSecs = CMTimeGetSeconds(currentTime); const Float64 currentTimeSecs = CMTimeGetSeconds(currentTime);
if( currentTimeSecs >= 0) { if( currentTimeSecs >= 0 && self.onVideoProgress) {
self.onVideoProgress(@{ self.onVideoProgress(@{
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(currentTime)], @"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(currentTime)],
@"playableDuration": [self calculatePlayableDuration], @"playableDuration": [self calculatePlayableDuration],
@ -277,12 +277,15 @@ static NSString *const playbackRate = @"rate";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//Perform on next run loop, otherwise onVideoLoadStart is nil //Perform on next run loop, otherwise onVideoLoadStart is nil
if(self.onVideoLoadStart) {
self.onVideoLoadStart(@{@"src": @{ self.onVideoLoadStart(@{@"src": @{
@"uri": [source objectForKey:@"uri"], @"uri": [source objectForKey:@"uri"],
@"type": [source objectForKey:@"type"], @"type": [source objectForKey:@"type"],
@"isNetwork": [NSNumber numberWithBool:(bool)[source objectForKey:@"isNetwork"]]}, @"isNetwork": [NSNumber numberWithBool:(bool)[source objectForKey:@"isNetwork"]]},
@"target": self.reactTag @"target": self.reactTag
}); });
}
}); });
} }
@ -342,6 +345,7 @@ static NSString *const playbackRate = @"rate";
orientation = @"portrait"; orientation = @"portrait";
} }
if(self.onVideoLoad) {
self.onVideoLoad(@{@"duration": [NSNumber numberWithFloat:duration], self.onVideoLoad(@{@"duration": [NSNumber numberWithFloat:duration],
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(_playerItem.currentTime)], @"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(_playerItem.currentTime)],
@"canPlayReverse": [NSNumber numberWithBool:_playerItem.canPlayReverse], @"canPlayReverse": [NSNumber numberWithBool:_playerItem.canPlayReverse],
@ -356,10 +360,12 @@ static NSString *const playbackRate = @"rate";
@"orientation": orientation @"orientation": orientation
}, },
@"target": self.reactTag}); @"target": self.reactTag});
}
[self attachListeners]; [self attachListeners];
[self applyModifiers]; [self applyModifiers];
} else if(_playerItem.status == AVPlayerItemStatusFailed) { } else if(_playerItem.status == AVPlayerItemStatusFailed && self.onVideoError) {
self.onVideoError(@{@"error": @{@"code": [NSNumber numberWithInteger: _playerItem.error.code], self.onVideoError(@{@"error": @{@"code": [NSNumber numberWithInteger: _playerItem.error.code],
@"domain": _playerItem.error.domain}, @"domain": _playerItem.error.domain},
@"target": self.reactTag}); @"target": self.reactTag});
@ -377,17 +383,21 @@ static NSString *const playbackRate = @"rate";
} }
} else if (object == _playerLayer) { } else if (object == _playerLayer) {
if([keyPath isEqualToString:readyForDisplayKeyPath] && [change objectForKey:NSKeyValueChangeNewKey]) { if([keyPath isEqualToString:readyForDisplayKeyPath] && [change objectForKey:NSKeyValueChangeNewKey]) {
if([change objectForKey:NSKeyValueChangeNewKey]) { if([change objectForKey:NSKeyValueChangeNewKey] && self.onReadyForDisplay) {
self.onReadyForDisplay(@{@"target": self.reactTag}); self.onReadyForDisplay(@{@"target": self.reactTag});
} }
} }
} else if (object == _player) { } else if (object == _player) {
if([keyPath isEqualToString:playbackRate]) { if([keyPath isEqualToString:playbackRate]) {
if(self.onPlaybackRateChange) {
self.onPlaybackRateChange(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate], self.onPlaybackRateChange(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate],
@"target": self.reactTag}); @"target": self.reactTag});
}
if(_playbackStalled && _player.rate > 0) { if(_playbackStalled && _player.rate > 0) {
if(self.onPlaybackResume) {
self.onPlaybackResume(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate], self.onPlaybackResume(@{@"playbackRate": [NSNumber numberWithFloat:_player.rate],
@"target": self.reactTag}); @"target": self.reactTag});
}
_playbackStalled = NO; _playbackStalled = NO;
} }
} }
@ -411,13 +421,17 @@ static NSString *const playbackRate = @"rate";
- (void)playbackStalled:(NSNotification *)notification - (void)playbackStalled:(NSNotification *)notification
{ {
if(self.onPlaybackStalled) {
self.onPlaybackStalled(@{@"target": self.reactTag}); self.onPlaybackStalled(@{@"target": self.reactTag});
}
_playbackStalled = YES; _playbackStalled = YES;
} }
- (void)playerItemDidReachEnd:(NSNotification *)notification - (void)playerItemDidReachEnd:(NSNotification *)notification
{ {
if(self.onVideoEnd) {
self.onVideoEnd(@{@"target": self.reactTag}); self.onVideoEnd(@{@"target": self.reactTag});
}
if (_repeat) { if (_repeat) {
AVPlayerItem *item = [notification object]; AVPlayerItem *item = [notification object];
@ -489,9 +503,11 @@ static NSString *const playbackRate = @"rate";
if (CMTimeCompare(current, cmSeekTime) != 0) { if (CMTimeCompare(current, cmSeekTime) != 0) {
[_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) { [_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) {
if(self.onVideoSeek) {
self.onVideoSeek(@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)], self.onVideoSeek(@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
@"seekTime": [NSNumber numberWithFloat:seekTime], @"seekTime": [NSNumber numberWithFloat:seekTime],
@"target": self.reactTag}); @"target": self.reactTag});
}
}]; }];
_pendingSeek = false; _pendingSeek = false;
@ -573,11 +589,15 @@ static NSString *const playbackRate = @"rate";
if( viewController ) if( viewController )
{ {
_presentingViewController = viewController; _presentingViewController = viewController;
if(self.onVideoFullscreenPlayerWillPresent) {
self.onVideoFullscreenPlayerWillPresent(@{@"target": self.reactTag}); self.onVideoFullscreenPlayerWillPresent(@{@"target": self.reactTag});
}
[viewController presentViewController:_playerViewController animated:true completion:^{ [viewController presentViewController:_playerViewController animated:true completion:^{
_playerViewController.showsPlaybackControls = YES; _playerViewController.showsPlaybackControls = YES;
_fullscreenPlayerPresented = fullscreen; _fullscreenPlayerPresented = fullscreen;
if(self.onVideoFullscreenPlayerDidPresent) {
self.onVideoFullscreenPlayerDidPresent(@{@"target": self.reactTag}); self.onVideoFullscreenPlayerDidPresent(@{@"target": self.reactTag});
}
}]; }];
} }
} }
@ -655,7 +675,7 @@ static NSString *const playbackRate = @"rate";
- (void)videoPlayerViewControllerWillDismiss:(AVPlayerViewController *)playerViewController - (void)videoPlayerViewControllerWillDismiss:(AVPlayerViewController *)playerViewController
{ {
if (_playerViewController == playerViewController && _fullscreenPlayerPresented) if (_playerViewController == playerViewController && _fullscreenPlayerPresented && self.onVideoFullscreenPlayerWillDismiss)
{ {
self.onVideoFullscreenPlayerWillDismiss(@{@"target": self.reactTag}); self.onVideoFullscreenPlayerWillDismiss(@{@"target": self.reactTag});
} }
@ -668,9 +688,11 @@ static NSString *const playbackRate = @"rate";
_fullscreenPlayerPresented = false; _fullscreenPlayerPresented = false;
_presentingViewController = nil; _presentingViewController = nil;
[self applyModifiers]; [self applyModifiers];
if(self.onVideoFullscreenPlayerDidDismiss) {
self.onVideoFullscreenPlayerDidDismiss(@{@"target": self.reactTag}); self.onVideoFullscreenPlayerDidDismiss(@{@"target": self.reactTag});
} }
} }
}
#pragma mark - React View Management #pragma mark - React View Management