2016-04-02 61 views
2

我試圖啓用使用AVPlayerLayer播放的視頻的慢播放速率。爲此,文檔指出,如果AVPlayerItem返回truecanPlaySlowForward,則啓用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) 
} 

,錄像文件不不包括音軌,但這仍然是必要的。

回答

3

撥打play()後設置rate。無需子類AVPlayerItem

func playVideo() { 
    let asset = AVAsset(URL: NSBundle.mainBundle().URLForResource("SampleVideo", withExtension: "mp4")!) 
    let playerItem = AVPlayerItem(asset: asset) 
    let player = AVPlayer(playerItem: playerItem) 
    let playerLayer = AVPlayerLayer(player: player) 
    playerLayer.frame = self.view.bounds 
    self.view.layer.addSublayer(playerLayer) 
    player.play() 
    player.rate = 0.5 
} 
相關問題