我试图抓住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,这就是全部.
但是,当我收到这些通知时,至少有2次.但我无法弄清楚每次播放停止时如何获取它们.
难道我做错了什么?