Initial commit for seeking

This commit is contained in:
Johannes Lumpe
2015-04-09 01:15:57 +03:00
parent ac3e4cf773
commit 45d79b701e
4 changed files with 70 additions and 10 deletions

View File

@@ -9,6 +9,7 @@
NSString *const RNVideoEventLoaded = @"videoLoaded";
NSString *const RNVideoEventLoading = @"videoLoading";
NSString *const RNVideoEventProgress = @"videoProgress";
NSString *const RNVideoEventSeek = @"videoSeek";
NSString *const RNVideoEventLoadingError = @"videoLoadError";
static NSString *const statusKeyPath = @"status";
@@ -23,6 +24,10 @@ static NSString *const statusKeyPath = @"status";
/* Required to publish events */
RCTEventDispatcher *_eventDispatcher;
bool _pendingSeek;
float _pendingSeekTime;
float _lastSeekTime;
/* For sending videoProgress events */
id _progressUpdateTimer;
int _progressUpdateInterval;
@@ -39,6 +44,10 @@ static NSString *const statusKeyPath = @"status";
_eventDispatcher = eventDispatcher;
_rate = 1.0;
_volume = 1.0;
_pendingSeek = false;
_pendingSeekTime = 0.0f;
_lastSeekTime = 0.0f;
}
return self;
}
@@ -169,8 +178,7 @@ static NSString *const statusKeyPath = @"status";
_playerLayer.videoGravity = mode;
}
- (void)setPaused:(BOOL)paused
{
- (void)setPaused:(BOOL)paused {
if (paused) {
[self stopProgressTimer];
[_player pause];
@@ -181,14 +189,54 @@ static NSString *const statusKeyPath = @"status";
}
}
- (void)setRate:(float)rate
{
- (void)setSeek:(NSDictionary*)seek {
NSNumber *seekValue = [seek objectForKey:@"time"];
if (!seekValue) {
return;
}
int timeScale = 10000;
float seekTime = [RCTConvert float:seekValue];
bool force = [RCTConvert BOOL:[seek objectForKey:@"force"]];
if (seekTime == _lastSeekTime && !force) {
return;
}
AVPlayerItem *item = _player.currentItem;
if (item && item.status == AVPlayerItemStatusReadyToPlay) {
// TODO check loadedTimeRanges
CMTime cmSeekTime = CMTimeMakeWithSeconds(seekTime, timeScale);
CMTime current = item.currentTime;
// TODO figure out a good tolerance level
CMTime tolerance = CMTimeMake(1000, timeScale);
if (CMTimeCompare(current, cmSeekTime) != 0) {
[_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance];
_pendingSeek = false;
_lastSeekTime = seekTime;
[_eventDispatcher sendInputEventWithName:RNVideoEventSeek body:@{
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],
@"seekTime": seekValue,
@"target": self.reactTag
}];
}
} else {
// TODO see if this makes sense and if so,
// actually implement it
_pendingSeek = true;
_pendingSeekTime = seekTime;
}
}
- (void)setRate:(float)rate {
_rate = rate;
[self applyModifiers];
}
- (void)setMuted:(BOOL)muted
{
- (void)setMuted:(BOOL)muted {
_muted = muted;
[self applyModifiers];
}
@@ -198,8 +246,7 @@ static NSString *const statusKeyPath = @"status";
[self applyModifiers];
}
- (void)applyModifiers
{
- (void)applyModifiers {
/* volume must be set to 0 if muted is YES, or the video freezes playback */
if (_muted) {
[_player setVolume:0];