AVPlayer设置iOS的初始播放时间

时间:2013-04-20 08:39:52

标签: iphone ios objective-c avplayer

KVO添加了AVPlayer何时播放视频queuePlayerAVPlayer

[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 videoseekToTime

stepByCount无法正常使用Values

编辑object中使用的methods correctno luck,即使{{1}}

1 个答案:

答案 0 :(得分:4)

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的{​​{1}} exact location {/ 1}}