可能是您在AVPlayerItem 的属性status 更改为.readToPlay 之前检查AVPlayerItem 的canPlayReverse 或canPlayFastForward。如果您这样做,您将始终收到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。以下是来自 Apple 的documentation:
AVPlayerItem 对象是动态的。的价值
对于所有基于文件的 AVPlayerItem.canPlayFastForward 将更改为 YES
资产和一些基于流媒体的资产(如果源播放列表提供
允许它的媒体)在项目准备好播放时。这
当播放器项目准备好播放时获得通知的方法是
通过 Key-Value Observing 观察 AVPlayerItem.status 属性
(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")
}