gpt4 book ai didi

ios - 为什么 AVPlayerItem 的 canPlayFastForward 方法返回 False?

转载 作者:可可西里 更新时间:2023-11-01 03:56:41 24 4
gpt4 key购买 nike

我很想用AVFoundation实现快进快退播放。

据我所知,如果 AVPlayerItem 的 canPlayReverse 和 canPlayFastForward 返回 False,我只能用 AVPlayer 播放 0.0 ~ 2.0 的速率。

但我需要 -1.0 并且评分超过 2.0。

我的问题是我无法找到何时为什么结果是假的。

Apple 的文档中没有提及 canPlayFastForward 何时返回 false。

谁能解释何时以及为什么 canPlayFastForwardcanPlayReverse 的结果是false 以及我该如何改变它是 true

最佳答案

可能是您在 AVPlayerItem 的属性之前检查了 AVPlayerItemcanPlayReversecanPlayFastForward status 更改为 .readToPlay。如果这样做,您将始终得到 false

不要这样做:

import AVFoundation

let anAsset = AVAsset(URL: <#A URL#>)
let playerItem = AVPlayerItem(asset: anAsset)
let canPlayFastForward = playerItem.canPlayFastForward
if (canPlayFastForward){
print("This line won't execute")
}

而是观察 AVPlayerItem 的属性 status。以下是documentation来自苹果:

AVPlayerItem objects are dynamic. The value of AVPlayerItem.canPlayFastForward will change to YES for all file-based assets and some streaming based assets (if the source playlist offers media that allows it) at the time the item becomes ready to play. The way to get notified when the player item is ready to play is by observing the AVPlayerItem.status property via Key-Value Observing (KVO).

import AVFoundation
dynamic var songItem:AVPlayerItem! //Make it instance variable

let anAsset = AVAsset(URL: <#A URL#>)
let songItem = AVPlayerItem(asset: anAsset)
playerItem.addObserver(self, forKeyPath: "status", options: .new, context: nil)

在同一个类中重写observeValue方法:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let status = change?[.newKey] as? Int{
if(status == AVPlayerItemStatus.readyToPlay.rawValue){
yourPlayer.rate = 2.0 // or whatever you want
}
}

}

不要忘记从 songItem 的状态观察器中删除此类

deinit {
playerItem.removeObserver(self, forKeyPath: "status")
}

关于ios - 为什么 AVPlayerItem 的 canPlayFastForward 方法返回 False?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611178/

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