2
我試圖啓用使用AVPlayerLayer
播放的視頻的慢播放速率。爲此,文檔指出,如果AVPlayerItem
返回true
爲canPlaySlowForward
,則啓用rate
的範圍爲0.0 - 1.0。此屬性是隻讀的,因此您需要繼承AVPlayerItem
的子類並覆蓋此屬性來完成此操作。我已經這樣做了,但視頻播放速度仍然不慢,總是1.0。它甚至從來沒有調用canPlaySlowForward
屬性。爲什麼是這樣?AVPlayerItem的canPlaySlowForward屬性從未調用過
import AVFoundation
class SlowMoPlayerItem: AVPlayerItem {
override var canPlaySlowForward: Bool {
return true
}
}
播放視頻:
let asset = AVAsset(URL: NSBundle.mainBundle().URLForResource("some-video", withExtension: "mp4")!)
let playerItem = SlowMoPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
player.rate = 0.5
player.muted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.layer.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
我做的視頻有關的唯一的其他東西/音頻是防止它停止後臺音頻,通過:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
}
,錄像文件不不包括音軌,但這仍然是必要的。