AVPlayer item buffer empty

断了今生、忘了曾经 提交于 2019-12-22 08:22:07

问题


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!


回答1:


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];
}



回答2:


@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];


来源:https://stackoverflow.com/questions/17942228/avplayer-item-buffer-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!