OGeek|极客世界-中国程序员成长平台

标题: ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:11
标题: ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL?

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

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

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

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

AVPlayerItem 异步加载内容,但 AVURLAsset 似乎没有这样做。



Best Answer-推荐答案


也有这个问题。解决了以下问题:

let asset = AVURLAsset(url: url)

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

// load values asynchronously and once complete, create the player item
let keys = ["duration", "tracks"]
asset.loadValuesAsynchronously(forKeys: keys, completionHandler: {
    let item = AVPlayerItem(asset: asset)
    self.player.insert(item, after: nil)
})

关于ios - AVURLAsset URLAssetWithURL ptions: blocks main thread with remote URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38777384/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4