Commit 2d1c0023 authored by Hampton Maxwell's avatar Hampton Maxwell
Browse files

Clear the progress timer at video end, restore on seek

parent f3aec372
Loading
Loading
Loading
Loading
+16 −7
Changes for ios/RCTVideo.m: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -124,6 +124,16 @@ static NSString *const timedMetadata = @"timedMetadata";
    return (kCMTimeRangeZero);
}

- (void)addPlayerTimeObserver
{
    const Float64 progressUpdateIntervalMS = _progressUpdateInterval / 1000;
    // @see endScrubbing in AVPlayerDemoPlaybackViewController.m of https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html
    __weak RCTVideo *weakSelf = self;
    _timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(progressUpdateIntervalMS, NSEC_PER_SEC)
                                                          queue:NULL
                                                     usingBlock:^(CMTime time) { [weakSelf sendProgressUpdate]; }
                     ];
}

/* Cancels the previously registered time observer. */
-(void)removePlayerTimeObserver
@@ -289,13 +299,7 @@ static NSString *const timedMetadata = @"timedMetadata";
  [_player addObserver:self forKeyPath:playbackRate options:0 context:nil];
  _playbackRateObserverRegistered = YES;

  const Float64 progressUpdateIntervalMS = _progressUpdateInterval / 1000;
  // @see endScrubbing in AVPlayerDemoPlaybackViewController.m of https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html
  __weak RCTVideo *weakSelf = self;
  _timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(progressUpdateIntervalMS, NSEC_PER_SEC)
                                                        queue:NULL
                                                   usingBlock:^(CMTime time) { [weakSelf sendProgressUpdate]; }
                   ];
  [self addPlayerTimeObserver];

  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
@@ -488,6 +492,8 @@ static NSString *const timedMetadata = @"timedMetadata";
    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero];
    [self applyModifiers];
  } else {
    [self removePlayerTimeObserver];
  }
}

@@ -567,6 +573,9 @@ static NSString *const timedMetadata = @"timedMetadata";
    if (CMTimeCompare(current, cmSeekTime) != 0) {
      if (!wasPaused) [_player pause];
      [_player seekToTime:cmSeekTime toleranceBefore:tolerance toleranceAfter:tolerance completionHandler:^(BOOL finished) {
        if (!_timeObserver) {
          [self addPlayerTimeObserver];
        }
        if (!wasPaused) [_player play];
        if(self.onVideoSeek) {
            self.onVideoSeek(@{@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(item.currentTime)],