Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer rate

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).

like image 314
user293895 Avatar asked Jun 21 '12 04:06

user293895


1 Answers

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.

like image 160
john.k.doe Avatar answered Oct 06 '22 05:10

john.k.doe