我正在一个播放音频流的应用程序中工作。我添加了观察者:playbackBufferEmpty 和playbackLikelyToKeepUp 以在互联网连接丢失时实现“缓冲”消息。这就是以下代码发生的情况:它开始播放没有问题,我看到“good to go”消息,如果我失去了连接,它会检测到playbackBufferEmpty,我看到“buffering”消息,但是当连接恢复时就像观察者丢失了,它甚至没有使用playbackLikelyToKeepUp 运行observeValueForKeyPath 方法。
这是我的代码:
- (void)viewDidLoad
{
…
NSString *urlstr = @"http://xxxxxxx.aac";
NSURL *url=[NSURL URLWithString:urlstr];
playerItem = [[AVPlayerItem playerItemWithURL:url]retain];
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]){
if (playerItem.playbackBufferEmpty) {
escuchando.text = @"buffering";
}
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
escuchando.text = @"good to go";
}
}
谢谢你的帮助!