2012-03-20 141 views
9

我想做一个基本的无线电应用程序,我得到了一个URL列表:当我尝试调用一个无效的URL(错误路径或没有可播放文件的正确路径)时观察员似乎从未被召唤过。这里是我的代码部分:AVPlayer观察者从未呼吁无效的URL

 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没有任何反应。 URL不工作的一个例子是:http://audioplayer.wunderground.com:80/RHBrant/Cheyenne.mp3.m3u

我在哪里错了?

非常感谢你。

回答

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; 
     } 

现在没关系。

Thanx