菜鸟教程小白 发表于 2022-12-11 18:15:01

iOS - AVPlayer 不为某些 URL 流式传输音频


                                            <p><p>我尝试过从流媒体播放音频。它适用于某些 URL,但对于某些具有某些 URL (<a href="http://listen.radionomy.com/almightyradiohindi" rel="noreferrer noopener nofollow">http://listen.radionomy.com/almightyradiohindi</a>) 的 URL,即使播放器状态为 = AVPlayerStatusReadyToPlay,它也会失败。如果我将相同的 URL 放在浏览器中不起作用,那么它也不起作用。但我认为播放器不应该将状态显示为 = AVPlayerStatusReadyToPlay。我面临管理它的问题。例如,我如何知道 AVPlayer 对哪个流媒体 URL 有效?</p>

<p>我的代码:</p>

<pre><code>-(void)viewDidLoad {
    NSURL *url = [ initWithString:urlString];
    AVAsset *asset = ;
    AVPlayerItem *anItem = ;
    [ setCategory:AVAudioSessionCategoryPlayback error:nil];
    avPlayer = ;
    ;

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == avPlayer &amp;&amp; ) {

      if (avPlayer.status == AVPlayerStatusFailed) {
            NSLog(@&#34;\n\n **** AVPlayer Failed***\n\n&#34;);
      }

      else if (avPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@&#34;\n\n ****AVPlayer Ready to Play   ****\n\n&#34;);
            ;

      }
      else if (avPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@&#34;\n\n**** AVPlayer Unknown\n\n ****&#34;);
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当我在浏览器中打开您的音频网址(<a href="http://listen.radionomy.com/almightyradiohindi" rel="noreferrer noopener nofollow">http://listen.radionomy.com/almightyradiohindi</a>)时,它会变为(<a href="http://streaming.radionomy.com/AlmightyRadioHindi?lang=en-US%2cen%3bq%3d0.8%2cfa%3bq%3d0.6" rel="noreferrer noopener nofollow">http://streaming.radionomy.com/AlmightyRadioHindi?lang=en-US%2cen%3bq%3d0.8%2cfa%3bq%3d0.6</a>)
如果你能得到准确的 url 并对其进行 url 编码,问题将得到解决</p>

<p>swift 3.0中的url编码:</p>

<pre><code>var originalString = &#34;test/test&#34;
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
print(escapedString!)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS - AVPlayer 不为某些 URL 流式传输音频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40945125/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40945125/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS - AVPlayer 不为某些 URL 流式传输音频