我使用播放流內容的AVPlayer。我想知道緩衝區爲空的時間以及準備播放的時間。但觀察者「playbackBufferEmpty」和「playbackLikelyToKeepUp」不會每次都按需要工作。他們有時工作,但往往不工作。我只在OSX 10.7.5下使用iPad模擬器iOS 6.1。 這是我如何設置,並聽取了觀察員:AVPlayer項緩衝區爲空
- (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)
{
...
}
}
}
請幫我用正確的方式來獲得「緩衝空」和「緩衝區準備就緒」事件(例如,當網絡連接斷開)。謝謝!