我尝试过从流媒体播放音频。它适用于某些 URL,但对于某些具有某些 URL (http://listen.radionomy.com/almightyradiohindi) 的 URL,即使播放器状态为 = AVPlayerStatusReadyToPlay,它也会失败。如果我将相同的 URL 放在浏览器中不起作用,那么它也不起作用。但我认为播放器不应该将状态显示为 = AVPlayerStatusReadyToPlay。我面临管理它的问题。例如,我如何知道 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)observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary *)change contextvoid *)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 ****");
}
}
}
当我在浏览器中打开您的音频网址(http://listen.radionomy.com/almightyradiohindi)时,它会变为(http://streaming.radionomy.com/AlmightyRadioHindi?lang=en-US%2cen%3bq%3d0.8%2cfa%3bq%3d0.6) 如果你能得到准确的 url 并对其进行 url 编码,问题将得到解决
swift 3.0中的url编码:
var originalString = "test/test"
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
print(escapedString!)
关于iOS - AVPlayer 不为某些 URL 流式传输音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40945125/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |