将观察者添加到AVPlayerItem

时间:2013-10-07 21:42:20

标签: avplayer observers

我正在使用播放音频流的应用。我添加了观察者:playbackBufferEmpty和playbackLikelyToKeepUp,以便在互联网连接丢失时实现“缓冲”消息。 以下代码会发生这种情况:它开始播放没有问题,我看到“好转”消息,如果我丢失连接它检测到playbackBufferEmpty,我看到“缓冲”消息,但是当连接回来时是就像观察者丢失一样,它甚至没有使用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";

    }
}

感谢您的帮助!

0 个答案:

没有答案