I use AVPlayer that plays stream content. I want to know the time when buffer is empty and the time when it is ready to play. But the observers "playbackBufferEmpty" and "playbackLikelyToKeepUp" do not work every time as it needed. They sometimes work but often do not work. I use only iPad simulator iOS 6.1 under OSX 10.7.5. Here is how i set and listen observers:
- (void)playAudioStream:(NSURL *)audioStreamURL
{
if(_audioPlayer && _audioPlayer.currentItem)
{
[_audioPlayer removeObserver:self forKeyPath:StatusKey];
[_audioPlayer.currentItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[_audioPlayer.currentItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
}
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:audioStreamURL];
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
_audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[_audioPlayer addObserver:self forKeyPath:StatusKey options:NSKeyValueObservingOptionNew context:nil];
//[_audioPlayer replaceCurrentItemWithPlayerItem:playerItem];
//_audioPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[_audioPlayer play];
}
...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"playbackBufferEmpty"] )
{
if (_audioPlayer.currentItem.playbackBufferEmpty)
{
...
}
}
if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (_audioPlayer.currentItem.playbackLikelyToKeepUp)
{
...
}
}
}
Please help me with the correct way to get "buffer empty" and "buffer ready" events(For example, when the Internet connection is broken). Thank you!