6
votes

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!

2

2 Answers

2
votes

You need to use context when adding observer. Here is sample code for you.

Init context

static void *playbackLikelyToKeepUpKVOToken = &playbackLikelyToKeepUpKVOToken;
static void *playbackBufferEmpty = &playbackBufferEmpty;
static void *playbackBufferFull = &playbackBufferFull;

Add Observer

[self.playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty" context:playbackBufferEmpty];
[self.playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp" context:playbackLikelyToKeepUpKVOToken];
[self.playerItem removeObserver:self forKeyPath:@"playbackBufferFull" context:playbackBufferFull];

Handle

if (context == playbackLikelyToKeepUpKVOToken)
{
    if (self.playerItem.playbackLikelyToKeepUp)
    {
        NSLog(@"recieve playbackLikelyToKeepUp");
    }

}
else if (context == playbackBufferEmpty)
{
    if (self.playerItem.playbackBufferEmpty)
    {
        NSLog(@"recieve playbackBufferEmpty");
    }
}
else if(context == playbackBufferFull)
{
    NSLog(@"recieve playbackBufferFull");
}
else
{
    [super observeValueForKeyPath:path ofObject:object change:change context:context];
}
0
votes

@Nghia Tran answer helped me. He just confused with Add observers / Remove observers ( We need both)

Add Observers:

[self.playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew  context:playbackBufferEmpty];
[self.playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew  context:playbackLikelyToKeepUpKVOToken];
[self.playerItem addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew   context:playbackBufferFull];

Remove Observers:

[self.playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty" context:playbackBufferEmpty];
[self.playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp" context:playbackLikelyToKeepUpKVOToken];
[self.playerItem removeObserver:self forKeyPath:@"playbackBufferFull" context:playbackBufferFull];