I am trying to catch the moment when AVPlayer cannot continue playing if there are no media available (network too slow, signal loss, etc.). As described in the documentation and various examples, I use 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 start playback and turn off WiFi after that. Unfortunately, neither " playbackBufferEmpty " nor " AVPlayerItemPlaybackStalledNotification " comes. At the time of stopping playback, I get only one AVPlayerItemTimeJumpedNotification and that's it. However, when I received these notifications, it was at least 2 times. But I canβt understand how to get them every time the playback stops. Am I doing something wrong?
source share