7

So I'm trying to play a sound file at a different rate in iOS 5.1.1, and am having absolutely no luck. So far I have tried setting the rate of the AVAudioPlayer:

player = [[AVAudioPlayer alloc] initWithContentsOfURL:referenceURL error:&error];
player.enableRate = YES;
player.rate = 1.5;
player.numberOfLoops = 0;
player.delegate = self;
[player prepareToPlay];
[player play];

with no luck at all, the sound plays but just ignores the rate I give it. I have also tried AVPlayer:

avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
avPlayer.rate = 0.5;
[avPlayer play];

Again, it plays but just ignores the rate I set. I have tried a number of different audio files but for the sake of this thread I selected Rooster-mono.wav from this catalogue: http://sig.sapp.org/sounds/wave/

Has anybody had any success with changing the playback rate on iOS 5.1.1? Or does anybody know what I am missing here?

I am doing this to change the pitch slightly of some of my samples, I realise I could do this by using RemoteIO or something similar but that seems total overkill for what I am trying to achieve (a simple playback rate adjustment).

user293895
  • 1,465
  • 3
  • 22
  • 39
  • this mistake you did over here is that plauer.rate = 1.5f. You have to put "f" at the end of it otherwise it will not work – Sam B Jun 07 '13 at 15:03

4 Answers4

8

here is some code that i know works, just re-tested in an app i've been working on. as you mention, using setEnableRate: and setRate: will only work with iOS 5.0 and above. so i use respondsToSelector: to test on the device whether or not the device will accept the request:

_noticeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Rooster-mono" ofType:@"wav"]]
                                                      error:nil];;
if ([_noticeAudio respondsToSelector:@selector(setEnableRate:)])
    _noticeAudio.enableRate = YES;
if ([_noticeAudio respondsToSelector:@selector(setRate:)])
    _noticeAudio.rate = 2.0;

running on an iOS 5 device, it performs the double-rate successfully. running on iOS 4.3, it plays it at normal speed.

so, the only way you'll get the proper rate is if your device has iOS 5 on it.

john.k.doe
  • 7,533
  • 2
  • 37
  • 64
8

Modify the code to:

avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];

[avPlayer play]; //call play first

avPlayer.rate = 0.5; //then set rate
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
tom mic
  • 89
  • 3
3

This is how you do it.

rate value is between 0.1f - 2.0f

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;
[player play];
Todd Lehman
  • 2,880
  • 1
  • 26
  • 32
Sam B
  • 27,273
  • 15
  • 84
  • 121
1

Swift 2.0

let player = AVPlayer(URL: url)
player.play()
player.rate = 0.9
quemeful
  • 9,542
  • 4
  • 60
  • 69