菜鸟教程小白 发表于 2022-12-11 17:11:10

ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL?


                                            <p><p>我没有找到关于此的文档,但根据我的实践经验</p>

<pre><code>AVURLAsset *asset = ;
</code></pre>

<p>(其中 url 是 HLS 直播的远程 URL),如果网络中断或由于某种原因无法读取文件,将阻塞主线程。</p>

<p><strong>有其他人注意到这一点吗?</strong>我可能最终会更改我的设置以使用 GCD 在后台线程上构建。因为在任何时候无法加载视频时,UI 都会锁定。</p>

<p><code>AVPlayerItem</code> 异步加载内容,但 <code>AVURLAsset</code> 似乎没有这样做。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>也有这个问题。解决了以下问题:</p>

<pre><code>let asset = AVURLAsset(url: url)

// I&#39;m using a resource loader for my custom urls...
let loaderQueue = DispatchQueue(label: &#34;loader-queue&#34;, qos: .userInteractive)
asset.resourceLoader.setDelegate(self, queue: self.loaderQueue)

// load values asynchronously and once complete, create the player item
let keys = [&#34;duration&#34;, &#34;tracks&#34;]
asset.loadValuesAsynchronously(forKeys: keys, completionHandler: {
    let item = AVPlayerItem(asset: asset)
    self.player.insert(item, after: nil)
})
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38777384/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38777384/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL?