Act according to playbackLikelyToKeepUp changes
Closes #57 This commit also includes some code style updates
This commit is contained in:
parent
db402be640
commit
28d482852d
86
RCTVideo.m
86
RCTVideo.m
@ -13,12 +13,13 @@ NSString *const RNVideoEventLoadingError = @"videoLoadError";
|
|||||||
NSString *const RNVideoEventEnd = @"videoEnd";
|
NSString *const RNVideoEventEnd = @"videoEnd";
|
||||||
|
|
||||||
static NSString *const statusKeyPath = @"status";
|
static NSString *const statusKeyPath = @"status";
|
||||||
|
static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp";
|
||||||
|
|
||||||
@implementation RCTVideo
|
@implementation RCTVideo
|
||||||
{
|
{
|
||||||
AVPlayer *_player;
|
AVPlayer *_player;
|
||||||
AVPlayerItem *_playerItem;
|
AVPlayerItem *_playerItem;
|
||||||
BOOL _playerItemObserverSet;
|
BOOL _playerItemObserversSet;
|
||||||
AVPlayerLayer *_playerLayer;
|
AVPlayerLayer *_playerLayer;
|
||||||
NSURL *_videoURL;
|
NSURL *_videoURL;
|
||||||
|
|
||||||
@ -99,14 +100,11 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_prevProgressUpdateTime == nil ||
|
if (_prevProgressUpdateTime == nil || (([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
|
||||||
(([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
|
[_eventDispatcher sendInputEventWithName:RNVideoEventProgress
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventProgress body:@{
|
body:@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)],
|
||||||
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)],
|
|
||||||
@"playableDuration": [self calculatePlayableDuration],
|
@"playableDuration": [self calculatePlayableDuration],
|
||||||
@"target": self.reactTag
|
@"target": self.reactTag}];
|
||||||
}];
|
|
||||||
|
|
||||||
_prevProgressUpdateTime = [NSDate date];
|
_prevProgressUpdateTime = [NSDate date];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +114,8 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
*
|
*
|
||||||
* \returns The playable duration of the current player item in seconds.
|
* \returns The playable duration of the current player item in seconds.
|
||||||
*/
|
*/
|
||||||
- (NSNumber *)calculatePlayableDuration {
|
- (NSNumber *)calculatePlayableDuration
|
||||||
|
{
|
||||||
AVPlayerItem *video = _player.currentItem;
|
AVPlayerItem *video = _player.currentItem;
|
||||||
if (video.status == AVPlayerItemStatusReadyToPlay) {
|
if (video.status == AVPlayerItemStatusReadyToPlay) {
|
||||||
__block CMTimeRange effectiveTimeRange;
|
__block CMTimeRange effectiveTimeRange;
|
||||||
@ -151,20 +150,22 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)addPlayerItemObserver
|
- (void)addPlayerItemObservers
|
||||||
{
|
{
|
||||||
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
|
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
|
||||||
_playerItemObserverSet = YES;
|
[_playerItem addObserver:self forKeyPath:playbackLikelyToKeepUpKeyPath options:0 context:nil];
|
||||||
|
_playerItemObserversSet = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fixes https://github.com/brentvatne/react-native-video/issues/43
|
/* Fixes https://github.com/brentvatne/react-native-video/issues/43
|
||||||
* Crashes caused when trying to remove the observer when there is no
|
* Crashes caused when trying to remove the observer when there is no
|
||||||
* observer set */
|
* observer set */
|
||||||
- (void)removePlayerItemObserver
|
- (void)removePlayerItemObservers
|
||||||
{
|
{
|
||||||
if (_playerItemObserverSet) {
|
if (_playerItemObserversSet) {
|
||||||
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
|
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
|
||||||
_playerItemObserverSet = NO;
|
[_playerItem removeObserver:self forKeyPath:playbackLikelyToKeepUpKeyPath];
|
||||||
|
_playerItemObserversSet = NO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,9 +173,9 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
|
|
||||||
- (void)setSrc:(NSDictionary *)source
|
- (void)setSrc:(NSDictionary *)source
|
||||||
{
|
{
|
||||||
[self removePlayerItemObserver];
|
[self removePlayerItemObservers];
|
||||||
_playerItem = [self playerItemForSource:source];
|
_playerItem = [self playerItemForSource:source];
|
||||||
[self addPlayerItemObserver];
|
[self addPlayerItemObservers];
|
||||||
|
|
||||||
[_player pause];
|
[_player pause];
|
||||||
[_playerLayer removeFromSuperlayer];
|
[_playerLayer removeFromSuperlayer];
|
||||||
@ -191,14 +192,12 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
[self.layer addSublayer:_playerLayer];
|
[self.layer addSublayer:_playerLayer];
|
||||||
self.layer.needsDisplayOnBoundsChange = YES;
|
self.layer.needsDisplayOnBoundsChange = YES;
|
||||||
|
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventLoading body:@{
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoading
|
||||||
@"src": @{
|
body:@{@"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
|
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source
|
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source
|
||||||
@ -223,6 +222,9 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
||||||
{
|
{
|
||||||
if (object == _playerItem) {
|
if (object == _playerItem) {
|
||||||
|
|
||||||
|
if ([keyPath isEqualToString:statusKeyPath]) {
|
||||||
|
// Handle player item status change.
|
||||||
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
|
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) {
|
||||||
float duration = CMTimeGetSeconds(_playerItem.asset.duration);
|
float duration = CMTimeGetSeconds(_playerItem.asset.duration);
|
||||||
|
|
||||||
@ -230,8 +232,8 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
duration = 0.0;
|
duration = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventLoaded body:@{
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoaded
|
||||||
@"duration": [NSNumber numberWithFloat:duration],
|
body:@{@"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],
|
||||||
@"canPlayFastForward": [NSNumber numberWithBool:_playerItem.canPlayFastForward],
|
@"canPlayFastForward": [NSNumber numberWithBool:_playerItem.canPlayFastForward],
|
||||||
@ -239,20 +241,23 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
@"canPlaySlowReverse": [NSNumber numberWithBool:_playerItem.canPlaySlowReverse],
|
@"canPlaySlowReverse": [NSNumber numberWithBool:_playerItem.canPlaySlowReverse],
|
||||||
@"canStepBackward": [NSNumber numberWithBool:_playerItem.canStepBackward],
|
@"canStepBackward": [NSNumber numberWithBool:_playerItem.canStepBackward],
|
||||||
@"canStepForward": [NSNumber numberWithBool:_playerItem.canStepForward],
|
@"canStepForward": [NSNumber numberWithBool:_playerItem.canStepForward],
|
||||||
@"target": self.reactTag
|
@"target": self.reactTag}];
|
||||||
}];
|
|
||||||
|
|
||||||
[self startProgressTimer];
|
[self startProgressTimer];
|
||||||
[self attachListeners];
|
[self attachListeners];
|
||||||
[self applyModifiers];
|
[self applyModifiers];
|
||||||
} else if(_playerItem.status == AVPlayerItemStatusFailed) {
|
} else if(_playerItem.status == AVPlayerItemStatusFailed) {
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventLoadingError body:@{
|
[_eventDispatcher sendInputEventWithName:RNVideoEventLoadingError
|
||||||
@"error": @{
|
body:@{@"error": @{
|
||||||
@"code": [NSNumber numberWithInteger: _playerItem.error.code],
|
@"code": [NSNumber numberWithInteger: _playerItem.error.code],
|
||||||
@"domain": _playerItem.error.domain
|
@"domain": _playerItem.error.domain},
|
||||||
},
|
@"target": self.reactTag}];
|
||||||
@"target": self.reactTag
|
}
|
||||||
}];
|
} else if ([keyPath isEqualToString:playbackLikelyToKeepUpKeyPath]) {
|
||||||
|
// Continue playing (or not if paused) after being paused due to hitting an unbuffered zone.
|
||||||
|
if (_playerItem.playbackLikelyToKeepUp) {
|
||||||
|
[self setPaused:_paused];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||||||
@ -270,9 +275,8 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
|
|
||||||
- (void)playerItemDidReachEnd:(NSNotification *)notification
|
- (void)playerItemDidReachEnd:(NSNotification *)notification
|
||||||
{
|
{
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventEnd body:@{
|
[_eventDispatcher sendInputEventWithName:RNVideoEventEnd body:@{@"target": self.reactTag}];
|
||||||
@"target": self.reactTag
|
|
||||||
}];
|
|
||||||
if (_repeat) {
|
if (_repeat) {
|
||||||
AVPlayerItem *item = [notification object];
|
AVPlayerItem *item = [notification object];
|
||||||
[item seekToTime:kCMTimeZero];
|
[item seekToTime:kCMTimeZero];
|
||||||
@ -316,19 +320,17 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
|
|
||||||
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) {
|
||||||
[_eventDispatcher sendInputEventWithName:RNVideoEventSeek body:@{
|
[_eventDispatcher sendInputEventWithName:RNVideoEventSeek
|
||||||
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
|
body:@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
|
||||||
@"seekTime": [NSNumber numberWithFloat:seekTime],
|
@"seekTime": [NSNumber numberWithFloat:seekTime],
|
||||||
@"target": self.reactTag
|
@"target": self.reactTag}];
|
||||||
}];
|
|
||||||
}];
|
}];
|
||||||
|
|
||||||
_pendingSeek = false;
|
_pendingSeek = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO see if this makes sense and if so,
|
// TODO: See if this makes sense and if so, actually implement it
|
||||||
// actually implement it
|
|
||||||
_pendingSeek = true;
|
_pendingSeek = true;
|
||||||
_pendingSeekTime = seekTime;
|
_pendingSeekTime = seekTime;
|
||||||
}
|
}
|
||||||
@ -407,7 +409,7 @@ static NSString *const statusKeyPath = @"status";
|
|||||||
[_playerLayer removeFromSuperlayer];
|
[_playerLayer removeFromSuperlayer];
|
||||||
_playerLayer = nil;
|
_playerLayer = nil;
|
||||||
|
|
||||||
[self removePlayerItemObserver];
|
[self removePlayerItemObservers];
|
||||||
|
|
||||||
_eventDispatcher = nil;
|
_eventDispatcher = nil;
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||||
|
Loading…
Reference in New Issue
Block a user