[英]iOS - AVPlayer not streaming audio for some URL
我嘗試播放流式播放的音頻。 對於某些URL,它可以正常工作,但是對於某些URL( http://listen.radionomy.com/almightyradiohindi ),即使播放器狀態變為= AVPlayerStatusReadyToPlay,它也會失敗。 如果我輸入了無法在瀏覽器中使用的相同URL,則在那里也將不起作用。 但我認為播放器不應將狀態顯示為= AVPlayerStatusReadyToPlay。 我面臨管理問題。 例如,我如何知道哪種流URL AVPlayer可以工作?
我的代碼:
-(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 ****");
}
}
}
當我在瀏覽器中打開您的音頻URL( http://listen.radionomy.com/almightyradiohindi )時,它將更改為( http://streaming.radionomy.com/AlmightyRadioHindi?lang=zh-CN%2cen%3bq%3d0。 8%2cfa%3bq%3d0.6 ),如果您可以獲取確切的網址並對其進行網址編碼,則該問題將得到解決
swift 3.0中的url編碼:
var originalString = "test/test"
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
print(escapedString!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.