SeektoTime无法正常工作

时间:2016-11-12 10:04:03

标签: ios objective-c avplayerviewcontroller

暂停和恢复视频我使用下面的代码。但它没有从它离开的地方恢复。有人可以帮助我吗?

-(void)appEnteredBackground:(NSNotification*)notification {
    [playerViewController.player pause];
    currentTime = [playerViewController.player currentTime];
}

-(void)appEnteredForeground:(NSNotification*)notification {

    if(playerViewController.player.status == AVPlayerStatusReadyToPlay &&
       self.playerViewController.player.currentItem.status ==  AVPlayerItemStatusReadyToPlay) {
       total_duration = self.playerViewController.player.currentItem.duration;
       [self.playerViewController.player.currentItem seekToTime:CMTimeMakeWithSeconds(CMTimeGetSeconds(currentTime),60000)];
       [playerViewController.player play];
    }
}

1 个答案:

答案 0 :(得分:-1)

如果您的currentTime值仍然存在,那么您需要添加公差部分以从精确的秒恢复。

[self.playerViewController.player.currentItem seekToTime:CMTimeMakeWithSeconds(CMTimeGetSeconds(currentTime),60000) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];