4

添加KVOAVPlayer何时按原样播放queuePlayer视频AVPlayer

[self.queuePlayer addObserver:self forKeyPath:@"status" options:0 context:NULL];

observer method

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if ([keyPath isEqualToString:@"status"]) {
    if (self.queuePlayer.status == AVPlayerStatusReadyToPlay) {

        NSInteger step = (NSInteger)(startTimeForVideo/0.04);
        [self.queuePlayer.currentItem stepByCount:step];

        //CMTime seekTime = CMTimeMake(startTimeForVideo*timeScale,timeScale);
        //if (CMTIME_IS_VALID(seekTime)) 
        //  [self.queuePlayer seekToTime:seekTime toleranceBefore:kCMTimePositiveInfinity toleranceAfter:kCMTimePositiveInfinity];
        //else
        //  NSLog(@"In valid time");

        [self.queuePlayer play];
    } else if (self.queuePlayer.status == AVPlayerStatusFailed) {
        /* An error was encountered */
    }
}

这里startTimeForVideo最初playBack time用于video

seekToTime也不工作stepByCount

编辑:用于Values是和即使objectmethodscorrectno luck

4

1 回答 1

5

KVO方法queuePlayer的变化AVPlayer

if (self.queuePlayer.status == AVPlayerStatusReadyToPlay) 
{
  //firstly make cmtime as here startTimeForVideo is Float64 value in seconds
  //get video's time scale as CMTime has it. 
  int32_t timeScale = self.queuePlayer.currentItem.asset.duration.timescale
  CMTime seektime=CMTimeMakeWithSeconds(startTimeForVideo, timeScale);
  //use this cmtime for seekToTime. 
  [self.queuePlayer seekToTime:seektime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
}
............

编辑:在这kCMTimeZero两种tolerance field方法exact locationseekToTime: toleranceBefore: toleranceAfter:

于 2013-04-22T09:40:51.683 回答