Member-only story

How to change the play speed rate on AVPlayer (swift)

Fredric Cliver
Oct 31, 2020

--

When you play audio with the AVPlayer, you would use this

player = AVPlayer.init(playerItem: playerItem)
player?.play()

There a specific property to calibrate the audio play speed rate. You can use the ‘rate’ property.

player = AVPlayer.init(playerItem: playerItem)
player?.rate = 0.7
player?.play()

But, in this case, it doesn’t work. You have to place after “.play()”

player = AVPlayer.init(playerItem: playerItem)
player?.play()
player?.rate = 0.7

After this, you play the sound, and you are able to recognize it’s not really okay. Because the sound is nasty. and You can hear the broken frame

You should set the “audioTimePitchAlgorithm” property manually.

player = AVPlayer.init(playerItem: playerItem)
player?.currentItem?.audioTimePitchAlgorithm = .timeDomain
player?.play()
player?.rate = 0.7

audioTimePitchAlgorithm has four different options.

Type Properties

static let lowQualityZeroLatency: AVAudioTimePitchAlgorithm
Low quality and very low computationally intensive pitch algorithm.

static let spectral: AVAudioTimePitchAlgorithm
The highest quality, most computationally expensive pitch algorithm.

static let timeDomain: AVAudioTimePitchAlgorithm
A modest quality pitch algorithm that is less computationally intensive than the spectral algorithm.

static let varispeed: AVAudioTimePitchAlgorithm
A high quality, no pitch correction algorithm.

Here is the comparison video.
As you can see, the only available option is just using ‘timeDomain’

--

--

Fredric Cliver
Fredric Cliver

Written by Fredric Cliver

13+ years in the digital trenches. I decode complex tech concepts into actionable insights, focusing on AI, Software Engineering, and emerging technologies.

No responses yet

Write a response