【问题标题】:iOS - AVPlayer not streaming audio for some URLiOS - AVPlayer 不为某些 URL 流式传输音频
【发布时间】:2016-12-03 06:54:33
【问题描述】:

我尝试过从流媒体播放音频。它适用于某些 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)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]; [玩家玩];
  • 你发现问题了吗?
  • @Ravi 你有什么解决办法吗?

标签: ios objective-c audio buffer avplayer


【解决方案1】:

当我在浏览器中打开您的音频 url(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!)

【讨论】:

  • @Hamid,更改后的 URL 也无法在浏览器中播放。所以这里不需要编码/解码。
猜你喜欢
  • 2012-01-18
  • 2012-10-19
  • 2011-07-06
  • 2016-01-05
  • 1970-01-01
  • 2012-12-21
  • 2012-07-29
  • 2016-09-29
  • 1970-01-01
相关资源
最近更新 更多