使用AVPlayer的seek方法后,需要等待AVPlayerItem的status状态变为AVPlayerItemStatusReadyToPlay时再开始播放,以保证音视频同步。代码示例如下:
// 注册监听status变化
[self.player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
// 在KVO方法中处理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
AVPlayerItemStatus status = [change[NSKeyValueChangeNewKey] integerValue];
if (status == AVPlayerItemStatusReadyToPlay) {
[self.player play];
}
}
}