2013-04-20 77 views
2

新增KVOAVPlayer時播放視頻作爲queuePlayerAVPlayerAVPlayer設置的初始播放時間的iOS

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

observermethod

-(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 INTIAL playBack timevideo

seekToTime不工作也不stepByCount

編輯:中objectValues用於methodscorrect而且即使no luck

回答

3

變化KVO方法queuePlayerAVPlayer

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]; 
} 
............ 

編輯:這裏都在tolerance field for exact location in seekToTime: toleranceBefore: toleranceAfter:方法

相關問題