[英]AVQueuePlayer/AVPlayer loading notification?
I have an AVQueuePlayer
(which obviously extends AVPlayer
) that loads a playlist of streaming audio. 我有一个
AVQueuePlayer
(显然扩展了AVPlayer
),可以加载流媒体音频的播放列表。 Streaming is all working fine, but I'd like to have an activity indicator to show the user audio is loading. 流媒体工作正常,但我想有一个活动指示器来显示用户音频正在加载。 Trouble is, I can't seem to find any such Notification in
AVQueuePlayer
(or AVPlayer
) that would indicate when the audio buffer has finished loading/is ready to play (nor does there appear to be a delegate method). 麻烦的是,我似乎无法在
AVQueuePlayer
(或AVPlayer
)中找到任何此类通知,指示音频缓冲区何时完成加载/准备播放(似乎也没有委托方法)。 Any thoughts? 有什么想法吗?
You will have to use KVO to get this done. 你必须使用KVO来完成这项工作。
For each item you are adding to the queue, you may setup observers like this: 对于要添加到队列的每个项目,您可以设置如下观察者:
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];
Now you can evaluate the status of that item within the observer method; 现在,您可以在observer方法中评估该项目的状态;
- (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");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.