[英]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.