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:
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];
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.
Swift 2.0
let player = AVPlayer(URL: url)
player.play()
player.rate = 0.9
Modify the code to:
avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
[avPlayer play]; //call play first
avPlayer.rate = 0.5; //then set rate