我正在尝试捕捉一个时刻,当AVPlayer
无法继续播放因为没有更多的媒体可用(网络过慢、信号丢失等)。根据文档和不同的示例,我正在使用KVO
来检测这一点:
item = [[AVPlayerItem alloc] initWithURL:audioURL];
player = [AVPlayer playerWithPlayerItem:item];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onItemNotification:) name:AVPlayerItemPlaybackStalledNotification object:item];
[item addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
...
- (void) onItemNotification:(NSNotification*)not
{
NSLog(@"Item notification: %@", not.name);
}
...
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
NSLog(@"Observe keyPath %@", keyPath);
}
我开始播放后把WiFi
关掉,但是不幸的是没有收到'playbackBufferEmpty
'或者'AVPlayerItemPlaybackStalledNotification
'。只有在播放停止时,我才会收到一个AVPlayerItemTimeJumpedNotification
通知,其他都没有了。
然而,至少有两次我收到了这些通知。但我不知道如何每次都在播放卡顿时获取它们。
我做错了什么吗?