当AVPlayer播放来自网络流的资源时,它会在到达下载内容的末尾时暂停.
所以,我的问题是,如何知道它因网络不良而停止了?如何在下载之后播放它,比方说,下一个10秒的资产?
解决方法
您可以在AVPlayer获取空缓冲区时添加观察者:
[[self.tracksPlayer currentItem] addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
还有一个观察者,这样你就可以知道AVPlayer何时缓冲到足以跟上:
[[self.tracksPlayer currentItem] addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
然后在KVO回调中检查一下:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == [self.tracksPlayer currentItem] && [keyPath isEqualToString:@"playbackBufferEmpty"]) { if ([self.tracksPlayer currentItem].playbackBufferEmpty) { NSLog(@"Buffer Empty"); } } else if (object == [self.tracksPlayer currentItem] && [keyPath isEqualToString:@"playbackLikelyToKeepUp"]) { if ([self.tracksPlayer currentItem].playbackLikelyToKeepUp) { NSLog(@"LikelyToKeepUp"); } } }