gpt4 book ai didi

ios - 使用 automaticallyLoadedAssetKeys 和 "duration"初始化 AVPlayerItem

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:10 38 4
gpt4 key购买 nike

我正在初始化一个 AVPlayerItem,它有一个参数 automaticallyLoadedAssetKeys,它指示在加载之前需要加载哪些 AVAsset 键标记为“准备播放”。

我见过有人将 duration 属性作为这些键之一传递的示例。

但是在 AVPlayerItem 的文档中,它讨论了 duration 属性,以及如何确保它有效。

The value of this property will be reported as kCMTimeIndefinite until the duration of the underlying asset has been loaded. There are two ways to ensure that the value of duration is accessed only after it becomes available:

Wait until the status of the player item is readyToPlay

另一种方法是注册一个 KVO,这很好。但是这第一条评论向我表明我不需要要求自动加载duration,因为它应该已经加载了。(?)

如果这是真的,我不明白为什么有人会传入 duration 作为自动加载的标志,因为看起来就是这样。我在这里遗漏了什么吗?

最佳答案

有些人这样做是因为存在 iOS 错误(包括在 iOS 12.1.4 中),有时会导致 AVPlayerItem.duration 为 nan,即使状态为 readyToPlay。示例 herehere .

但是,我的经验是在 automaticallyLoadedAssetKeys 中指定“duration”并不能解决问题——尽管 Apple 的文档 AVPlayerItem :

The value of each key in automaticallyLoadedAssetKeys will automatically be loaded by the underlying AVAsset before the player item achieves the status AVPlayerItem.Status.readyToPlay; i.e. when the item is ready to play, the value returned by invoking the asset property’s statusOfValue(forKey:error:) method will be one of the terminal status values

我可以获得 AVPlayerItem.Status.readyToPlay,其中 statusOfValue(forKey: "duration") 返回 .loaded,持续时间为 nan。

我发现每次都有效的唯一解决方案是同时 KVO AVPlayerItem.status(对于 .readyToPlay)和 AVPlayerItem.duration(对于有效数字),并且当任一触发时,检查两者:

    if item.status != AVPlayerItem.Status.readyToPlay || !item.duration.isUsableNumber() {
return // not ready yet (for addBoundaryTimeObserver, seek to end, etc.)
}

isUsableNunber() 只是:

 func isUsableNumber() -> Bool {
return isValid && isNumeric && !isIndefinite && !isNegativeInfinity && !isPositiveInfinity
}

关于ios - 使用 automaticallyLoadedAssetKeys 和 "duration"初始化 AVPlayerItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963946/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com