I'm trying to enable slow playback rate for a video I'm playing using AVPlayerLayer
. To do so the documentation states it will enable rate
in the range of 0.0 - 1.0 if the AVPlayerItem
returns true
for canPlaySlowForward
. This property is readonly, so you need to subclass AVPlayerItem
and override this property to accomplish this. I've done that, but the video still does not playing at a slow rate, it's always the 1.0 rate. It never even calls the canPlaySlowForward
property. Why is this?
import AVFoundation
class SlowMoPlayerItem: AVPlayerItem {
override var canPlaySlowForward: Bool {
return true
}
}
Playing the video:
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()
The only other thing I do related to video/audio is prevent it from stopping background audio, via:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
}
The video file does not include an audio track but this was still necessary to include.