2013-02-18 12 views
13

Sto usando AVPlayer per riprodurre audio da un URLstato AVPlayer sempre AVPlayerStatusReadyToPlay

In viewDidLoad:

self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:imageText]]; 

self.player = [AVPlayer playerWithPlayerItem:playerItem]; 

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

[player play]; 

Observer

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
         change:(NSDictionary *)change context:(void *)context { 
    if (object == player && [keyPath isEqualToString:@"status"]) { 
     if (player.status == AVPlayerStatusReadyToPlay) { 
      //[playingLbl setText:@"Playing Audio"]; 
      NSLog(@"fineee"); 
      [playBtn setEnabled:YES]; 
     } else if (player.status == AVPlayerStatusFailed) { 
      // something went wrong. player.error should contain some information 
      NSLog(@"not fineee"); 
      NSLog(@"%@",player.error); 

     } 
     else if (player.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 


     } 
    } 
} 

ma il giocatore a volte è bloccato e non lo fa riprodurre l'audio, ma anche lo stato è AVPlayerStatusReadyToPlay. Non entra mai in AVPlayerStatusFailed o AVPlayerItemStatusUnknown. Dato che voglio gestire l'errore di AVPlayer, deve entrare anche in questi. Per favore aiuto!!

risposta

31

Si dovrebbe osservare lo stato di CurrentItem. AVPlayer non è riuscito a causa di AVPlayerItem non riuscito, se qualcosa è andato storto, inizia da AVPlayerItem e AVPlayer.

prova:

[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil]; 

nel observeValueForKeyPath:

if (object == audioPlayer.currentItem && [keyPath isEqualToString:@"status"]) { 
    if (audioPlayer.currentItem.status == AVPlayerItemStatusFailed) { 
     NSLog(@"------player item failed:%@",audioPlayer.currentItem.error); 
    } 
} 

Si può prendere uno sguardo di gestione del AVPlayer o utilizzare direttamente da HysteriaPlayer, il mio progetto open source.

+0

Grazie super :) Sei il salvatore :) – Srikanth

+0

GRAZIE !!! Questo codice mi ha aiutato! –

+0

** Esattamente ** quello che stavo cercando, grazie! – zpasternack