使用automaticallyLoadedAssetKeys和“duration”初始化AVPlayerItem

3

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

我看到一些人的示例将duration属性作为其中一个关键字传递。

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

在底层资源的持续时间被加载之前,此属性的值将报告为kCMTimeIndefinite。有两种方法可以确保仅在其可用后访问持续时间的值:

等待播放器项目的状态为readyToPlay

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

如果这是正确的,我就无法理解为什么有人会将duration作为自动加载的标志传递,因为看起来是这样的。我在这里漏掉了什么吗?

2个回答

3
有些人这样做是因为iOS存在漏洞(包括在iOS 12.1.4中),有时会导致AVPlayerItem.duration的值为NaN,即使状态为readyToPlay。例如这里这里
然而,我的经验是在automaticallyLoadedAssetKeys中指定“duration”并不能解决这个问题 - 尽管苹果的文档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.)
    }

Where isUsableNunber() is just:

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

1
抱歉,我知道这有点过时。但是我最近找到了解决我的问题的答案。
所以,我意识到我混淆了文档,如果我更努力地查看,它可能一直在文档中,我会注意到的。 AVPlayerItem(asset: asset)默认加载duration属性。这就像调用AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: ["duration"]) 文档实际上已经说了这一点,但我完全忽略了它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接