I'm trying to catch a moment when AVPlayer
is unable to continue playback in case no more media available (too slow network, signal loss, etc). As described in documentation and different examples I'm using KVO
to detect this:
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);
}
I'm starting a playback and turn WiFi
off after that. Unfortunately neither 'playbackBufferEmpty
' nor 'AVPlayerItemPlaybackStalledNotification
' comes. At the moment when playback stops I receive only one AVPlayerItemTimeJumpedNotification
and that's all.
However there were at least 2 times when I got these notifications. But I can't figure out how to get them every time when playback is stalled.
Am I doing something wrong?
First try to disconnect internet from your router and you will get playbackBufferEmpty notification. To handle network switching you will need to implemented Reachability