Reputation: 1527
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).
Upvotes: 8
Views: 9079
Reputation: 9868
Swift 2.0
let player = AVPlayer(URL: url)
player.play()
player.rate = 0.9
Upvotes: 1
Reputation: 27618
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];
Upvotes: 3
Reputation: 89
Modify the code to:
avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
[avPlayer play]; //call play first
avPlayer.rate = 0.5; //then set rate
Upvotes: 8
Reputation: 7563
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.
Upvotes: 8