我试图抓住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次.但我无法弄清楚每次播放停止时如何获取它们.
难道我做错了什么?
解决方法
首先尝试断开互联网与路由器的连接,然后您将获得playbackBufferEmpty通知.
要处理网络切换,您需要实现 Reachability
要处理网络切换,您需要实现 Reachability