处理AVPlayer停顿

san*_*san 8 objective-c key-value-observing ios avplayer

我试图抓住一个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);
}
Run Code Online (Sandbox Code Playgroud)

我正在开始播放并WiFi在此之后关闭.不幸的是,playbackBufferEmpty"也没有AVPlayerItemPlaybackStalledNotification"来了.在播放停止的那一刻,我只收到一个AVPlayerItemTimeJumpedNotification,就是全部.但是,当我收到这些通知时,至少有2次.但我无法弄清楚每次播放停止时如何获取它们.难道我做错了什么?

Arm*_*s.A 2

首先尝试断开路由器与互联网的连接,您将收到playbackBufferEmpty 通知。要处理网络切换,您需要实现可达性

  • 可达性和播放停止是两个不同的东西。 (2认同)