AVPlayer
has a property called rate
that is meant to control the playback rate. 1.0
is normal speed while values like 2.0
or
This is only a workaround.
When the playback rate is changed from 0.0 to a large value, if this is the first zero-to-nonzero transition in playback rate since the last call to AVPlayer.replaceCurrentItem
, playback is smooth (and audio is automatically muted). It is necessary that this is the first such transition: merely setting the rate to 0.0 first and then to the desired rate does not work.
So, for example, this will produce smooth playback at high speeds:
func setPlayerRate(player: AVPlayer, rate: Float) {
// AVFoundation wants us to do most things on the main queue.
DispatchQueue.main.async {
if (rate == player.rate) {
return
}
if (rate > 2.0 || rate < -2.0) {
let playerItem = player.currentItem
player.replaceCurrentItem(with: nil)
player.replaceCurrentItem(with: playerItem)
player.rate = rate
} else {
// No problems "out of the box" with rates in the range [-2.0,2.0].
player.rate = rate
}
}
}