我曾嘗試從流播放音頻。它對一些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 ****");
}
}
}
NSURL * streamingURL = [NSURL URLWithString:URL_STRING]; NSLog(@「%@」,streamingURL); player = [AVPlayer playerWithURL:streamingURL]; [玩家播放]; –
你有沒有想過這個問題? – Artur