首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVURLAsset loadValuesAsynchronously从不失败

AVURLAsset loadValuesAsynchronously从不失败
EN

Stack Overflow用户
提问于 2017-09-14 22:42:39
回答 1查看 5.4K关注 0票数 5

我正在阅读&试用苹果公司关于加载视频资产的示例代码。

但我注意到,即使我在设备上打开飞机模式或在我的mac上关闭wifi,AVURLAsset上的AVURLAsset功能也不会失败。谁知道在什么情况下loadValuesAsynchronously将无法加载密钥?

这是相对的样本代码

代码语言:javascript
复制
asset.loadValuesAsynchronously(forKeys: PlayerViewController.assetKeysRequiredToPlay) {

    /*
        The asset invokes its completion handler on an arbitrary queue.
        To avoid multiple threads using our internal state at the same time
        we'll elect to use the main thread at all times, let's dispatch
        our handler to the main queue.
    */
    DispatchQueue.main.async() {
        /*
            This method is called when the `AVAsset` for our URL has 
            completed the loading of the values of the specified array 
            of keys.
        */

        /*
            Test whether the values of each of the keys we need have been
            successfully loaded.
        */
        for key in PlayerViewController.assetKeysRequiredToPlay {
            var error: NSError?

            if asset.statusOfValue(forKey: key, error: &error) == .failed {
                let stringFormat = NSLocalizedString("error.asset_%@_key_%@_failed.description", comment: "Can't use this AVAsset because one of it's keys failed to load")

                let message = String.localizedStringWithFormat(stringFormat, title, key)

                self.handleError(with: message, error: error)

                return
            }
        }

        // We can't play this asset.
        if !asset.isPlayable || asset.hasProtectedContent {
            let stringFormat = NSLocalizedString("error.asset_%@_not_playable.description", comment: "Can't use this AVAsset because it isn't playable or has protected content")

            let message = String.localizedStringWithFormat(stringFormat, title)

            self.handleError(with: message)

            return
        }

        /*
            We can play this asset. Create a new AVPlayerItem and make it
            our player's current item.
        */
        self.loadedAssets[title] = asset

        let name = (thumbnailResourceName as NSString).deletingPathExtension
        let type = (thumbnailResourceName as NSString).pathExtension
        let path = Bundle.main.path(forResource: name, ofType: type)!

        let thumbnail = UIImage(contentsOfFile: path)!

        self.assetTitlesAndThumbnails[asset.url] = (title, thumbnail)
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-15 02:04:53

最后,回答我自己的问题:我一直在使用HLS视频,网址是https://www.foo.com/master.m3u8格式的。loadValuesAsynchronously不会执行任何网络操作来确定该网址是否包含有效的HLS文件。这就是为什么“可玩”键总是有效的原因:

代码语言:javascript
复制
static let assetKeysRequiredToPlay = [
    "playable",
    "hasProtectedContent"
]

不过,如果我还添加了一些回放信息,如“持续时间”和“轨道”,那么如果没有任何网络连接,它就会失败,因为它将需要实际加载信息。更多细节请参阅以下答案:https://stackoverflow.com/a/31418812/1035008

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46229228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档