- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在初始化一个 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 isreadyToPlay
另一种方法是注册一个 KVO,这很好。但是这第一条评论向我表明我不需要要求自动加载duration
,因为它应该已经加载了。(?)
如果这是真的,我不明白为什么有人会传入 duration
作为自动加载的标志,因为看起来就是这样。我在这里遗漏了什么吗?
最佳答案
有些人这样做是因为存在 iOS 错误(包括在 iOS 12.1.4 中),有时会导致 AVPlayerItem.duration 为 nan,即使状态为 readyToPlay。示例 here和 here .
但是,我的经验是在 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/
我正在初始化一个 AVPlayerItem,它有一个参数 automaticallyLoadedAssetKeys,它指示在加载之前需要加载哪些 AVAsset 键标记为“准备播放”。 我见过有人将
我是一名优秀的程序员,十分优秀!