AVPlayer Observer从未调用过无效的URL

时间:2012-03-20 11:00:53

标签: ios avplayer

我正在尝试做一个基本的广播应用程序并且我得到了一个URL列表:当我尝试调用无效的URL(错误的路径或没有可播放文件的正确路径)时,Observer似乎永远不会被调用。这是我的代码的一部分:

        urlStream = [NSURL URLWithString:mp3URL];  

        self.playerItem = [AVPlayerItem playerItemWithURL:urlStream];

        [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty"   options:NSKeyValueObservingOptionNew context:nil];
        [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
        [appDelegate.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

        [appDelegate.player replaceCurrentItemWithPlayerItem:playerItem]; //OK

        [appDelegate.player play];

并且在我得到的相对观察者方法中:

    if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
    if (playerItem.playbackBufferEmpty) 
    {
        NSLog(@"playbackBufferEmpty");
        [self alertBuffer];
    }

}

if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
    if (playerItem.playbackLikelyToKeepUp)
    {
        NSLog(@"playbackLikelyToKeepUp");
        [loadStation stopAnimating];

        if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
            NSLog(@"AVPlayerItemStatusReadyToPlay");
        }
        else if (playerItem.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayerStatusFailed");
        }
        else if (playerItem.status == AVPlayerStatusUnknown) {
            NSLog(@"AVPlayerStatusUnknown");
        }
    }
}

else if (object == appDelegate.player && [keyPath isEqualToString:@"status"]) {
    if (appDelegate.player.status == AVPlayerStatusReadyToPlay) {
        NSLog(@"Player Status = Ready to Play");

    } 
    if (appDelegate.player.status == AVPlayerStatusUnknown) {
        NSLog(@"Player Status = Sometimes did wrong.");
        [self alertUnknown];
    }
    if (appDelegate.player.status == AVPlayerStatusFailed) {
        NSLog(@"Player Status = Status Failed.");
        [self alertStatusFailed];
    }

}

无论我调用哪个URL,我都只获得状态ReadyToPlay:当我选择无效的URL时,没有任何反应。 无效网址的示例是:http://audioplayer.wunderground.com:80/RHBrant/Cheyenne.mp3.m3u

我哪里错了?

非常感谢你。

1 个答案:

答案 0 :(得分:10)

好的,我发现了我的错误:我正在向appDelegate.player添加观察者以捕获错误的URL,而我必须做的就是添加到playerItem:

        [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];

并以相对方法:

         if ([playerItem status] == AVPlayerStatusFailed) {
             NSLog(@"playerItem Status = Failed.");
             NSLog(@"Error = %@",error.description);
             return;
         }

现在没关系。

感谢名单