我有一个AVQueuePlayer
(显然会扩展AVPlayer
),它会加载流媒体音频的播放列表。流媒体工作正常,但我想有一个活动指示器来显示用户音频正在加载。麻烦的是,我似乎无法在AVQueuePlayer
(或AVPlayer
)中找到任何此类通知,表明音频缓冲区何时完成加载/准备播放(似乎也没有代表方法)。有什么想法吗?
答案 0 :(得分:26)
你必须使用KVO来完成这项工作。
对于要添加到队列的每个项目,您可以设置如下观察者:
item_ = [[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://somefunkyurl"]] retain];
[item_ addObserver:self forKeyPath:@"status" options:0 context:nil];
[item_ addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];
现在,您可以在观察者方法中评估该项目的状态;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([object isKindOfClass:[AVPlayerItem class]])
{
AVPlayerItem *item = (AVPlayerItem *)object;
//playerItem status value changed?
if ([keyPath isEqualToString:@"status"])
{ //yes->check it...
switch(item.status)
{
case AVPlayerItemStatusFailed:
NSLog(@"player item status failed");
break;
case AVPlayerItemStatusReadyToPlay:
NSLog(@"player item status is ready to play");
break;
case AVPlayerItemStatusUnknown:
NSLog(@"player item status is unknown");
break;
}
}
else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
{
if (item.playbackBufferEmpty)
{
NSLog(@"player item playback buffer is empty");
}
}
}
}