为什么 AVPlayerItem 的 canPlayFastForward 方法 returns False?
Why AVPlayerItem's canPlayFastForward method returns False?
很想用AVFoundation实现快进快退。
据我所知,如果 AVPlayerItem 的 canPlayReverse 和 canPlayFastForward returns False,我只能用 AVPlayer 播放 0.0 ~ 2.0 的速率。
但我需要-1.0 并且评分超过 2.0。
我的问题是我找不到 when 和 why 结果是错误的。
Apple 的文档中没有提及 canPlayFastForward returns 何时为 false。
谁能解释何时以及为什么 canPlayFastForward & canPlayReverse 的结果是false 以及如何将其更改为 true?
可能是您在 AVPlayerItem
的 属性 status
更改为之前检查 AVPlayerItem
的 canPlayReverse
或 canPlayFastForward
.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
。以下是 Apple 的 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)
在同一个 class:
中重写 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 的状态观察器中删除这个 class
deinit {
playerItem.removeObserver(self, forKeyPath: "status")
}
很想用AVFoundation实现快进快退。
据我所知,如果 AVPlayerItem 的 canPlayReverse 和 canPlayFastForward returns False,我只能用 AVPlayer 播放 0.0 ~ 2.0 的速率。
但我需要-1.0 并且评分超过 2.0。
我的问题是我找不到 when 和 why 结果是错误的。
Apple 的文档中没有提及 canPlayFastForward returns 何时为 false。
谁能解释何时以及为什么 canPlayFastForward & canPlayReverse 的结果是false 以及如何将其更改为 true?
可能是您在 AVPlayerItem
的 属性 status
更改为之前检查 AVPlayerItem
的 canPlayReverse
或 canPlayFastForward
.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
。以下是 Apple 的 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)
在同一个 class:
中重写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 的状态观察器中删除这个 class
deinit {
playerItem.removeObserver(self, forKeyPath: "status")
}