23

I want to control the playback speed of audio in AVAudioplayer. Is this possible? If so, how would you do it?

TechZen
  • 64,370
  • 15
  • 118
  • 145
jecob
  • 245
  • 1
  • 3
  • 5
  • 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 Answers6

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
  • 1
    I 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
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
    As of 2014 (and probably much earlier), this is not true. – Kevin Mar 16 '14 at 20:45
  • 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)

0

AVAudioPlayer does not support playback speed setting. Audio Queue Services are quite a pain to use, so that you might want to try OpenAL. See the sound engine from Cocos2D or Finch for examples of how to wrap OpenAL in Objective-C.

zoul
  • 102,279
  • 44
  • 260
  • 354