菜鸟教程小白 发表于 2022-12-11 20:44:41

ios - HLS 视频无法在模拟器和真实设备上播放


                                            <p><p>我正在开发一个 iOS 应用,它只播放 HLS 直播视频。</p>

<p>我的问题是我已经使用 AVPlayer 和 ViewController 来设置 Playground ,一切正常, ViewController 已启动,播放器也已启动,但流未启动。该流是一种 .m3u8 类型,在 safari 和 chrome 中工作得非常好。 iOS 不会在模拟器或真实设备上显示视频。 </p>

<p>我还搜索了其他 SO 解决方案,但它们都没有对我有用。 </p>

<pre><code> /* Button to play live news streaming */
@IBAction func liveNews(_ sender: Any)
{
    guard let NewsUrl = URL(string: &#34;http://cdn39.live247stream.com/A1TVuk/tv/playlist.m3u8&#34;)
      else {
            return }

    /* Create an AV PLAYER and passed the HLS URL to it */
    let player = AVPlayer(url: NewsUrl)
    player.allowsExternalPlayback = true

    /* Setup a player view controller to handle the stream */
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player

    /* Using method of play() to load and play the stream*/
    present(playerViewController, animated: true){
    playerViewController.player?.play()
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我使用的是不 protectedHTTP URL,因此它不允许设备或模拟器播放它。我放了一个异常(exception),允许不安全的协议(protocol)让 iOS 流畅地流式传输 HLS。 </p>

<pre><code> &lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
    &lt;dict&gt;
      &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSExceptionDomains&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
      &lt;true/&gt;
    &lt;/dict&gt;
</code></pre>

<p> <a href="/image/EOChe.png" rel="noreferrer noopener nofollow"><img src="/image/EOChe.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - HLS 视频无法在模拟器和真实设备上播放,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53004636/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53004636/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - HLS 视频无法在模拟器和真实设备上播放