2016-12-03 56 views
1

我曾嘗試從流播放音頻。它對一些URL工作正常,但對於某些URL(http://listen.radionomy.com/almightyradiohindi),即使播放器狀態失敗,它也會失敗= AVPlayerStatusReadyToPlay。如果我把相同的URL在瀏覽器中不起作用,那麼它也不起作用。但我認爲玩家不應該將狀態顯示爲= AVPlayerStatusReadyToPlay。我面臨着管理它的問題。例如,我怎麼知道哪個流媒體URL AVPlayer將工作與否?iOS - AVPlayer沒有爲某些URL流式傳輸音頻

我的代碼:

-(void)viewDidLoad { 
    NSURL *url = [[NSURL alloc] initWithString:urlString]; 
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    avPlayer = [AVPlayer playerWithPlayerItem:anItem]; 
    [avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 

} 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if (object == avPlayer && [keyPath isEqualToString:@"status"]) { 

     if (avPlayer.status == AVPlayerStatusFailed) { 
      NSLog(@"\n\n **** AVPlayer Failed ***\n\n"); 
     } 

     else if (avPlayer.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"\n\n **** AVPlayer Ready to Play ****\n\n"); 
      [avPlayer play]; 

     } 
     else if (avPlayer.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"\n\n **** AVPlayer Unknown \n\n ****"); 
     } 
    } 
} 
+0

NSURL * streamingURL = [NSURL URLWithString:URL_STRING]; NSLog(@「%@」,streamingURL); player = [AVPlayer playerWithURL:streamingURL]; [玩家播放]; –

+0

你有沒有想過這個問題? – Artur

回答

0

當我在瀏覽器中打開你的音頻網址(http://listen.radionomy.com/almightyradiohindi)將變爲(http://streaming.radionomy.com/AlmightyRadioHindi?lang=en-US%2cen%3bq%3d0.8%2cfa%3bq%3d0.6) 如果你能得到確切的url和URL編碼,它的問題將

url編碼在迅速3.0:

var originalString = "test/test" 
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) 
print(escapedString!) 
+0

@哈米德,更改的URL也沒有在瀏覽器中播放。所以不需要在這裏編碼/解碼。 –