I want to control the playback speed of audio in AVAudioplayer
. Is this possible? If so, how would you do it?
Asked
Active
Viewed 2.0k times
23
-
Look into the API of AVAudioplayer. – Zaki Feb 28 '10 at 09:42
-
Looks like it works for iOS5 - [docs](http://developer.apple.com/library/IOS/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html) and look at **rate**. thanks, – Sergey Kopanev Nov 11 '11 at 20:33
6 Answers
35
Now it is possible to change the playback speed.
Sample code:
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&err];
player.volume = 0.4f;
player.enableRate = YES; //<--
[player prepareToPlay];
[player setNumberOfLoops:0];
player.rate = 2.0f; //<-- Playback Speed
[player play];
enableRate
is set to YES
and you can change it.
See more in the docs.

ricardopereira
- 11,118
- 5
- 63
- 81

james shen
- 491
- 4
- 5
-
1I can confirm that this works like a charm in iOS 6. Set enableRate to YES, and you can adjust the value of rate between 0.5 and 2.0 (half speed to double speed) with no pitch correction necessary. It can even be adjusted DURING playback, as long as you set enableRate before you start playing the audio. – Eli Burke Jun 25 '13 at 13:01
17
Swift 2 - Swift 5
You can increase the rate by doing this:
player.enableRate = true
player.prepareToPlay()
player.rate = 2.0
player.play()
If you want to loop, you can add this:
player.numberOfLoops = 3

Guy Daher
- 5,526
- 5
- 42
- 67
-
-
1
-
-
downvoted. `enableRate` should be set before calling `prepareToPlay()`. see [the docs](https://developer.apple.com/documentation/avfoundation/avaudioplayer/1387084-enablerate) – Adam Apr 04 '20 at 16:51
-
1Sure @Adam thanks for pointing it out. Next time, feel free to improve the answer by editing it. – Guy Daher Apr 08 '20 at 08:26
3
Try this -
audioP = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: selectedPath), fileTypeHint: "caf")
audioP.enableRate = true
audioP.prepareToPlay()
audioP.rate = 1.5
audioP.play()

Shawon91
- 200
- 3
- 9
1
You can't. You can only change the volume not the speed of playback.
I think to do that, you will have to use the much lower level Audio Queue APIs and manipulate the audio stream manually to apply that effect.

TechZen
- 64,370
- 15
- 118
- 145

Stefan Arentz
- 34,311
- 8
- 67
- 88
-
1
-
No it will now work fine, Check the doc: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Chapters/Reference.html#jumpTo_15 – Manab Kumar Mal Sep 01 '14 at 11:57
1
set enableRate = YES and then you can adjust the rate between 0.1 and 2.0 for slowed down and sped up (1.0 is normal speed)

studenttttt
- 21
- 4