AVAudioPlayer Level

So, I'm trying to play the sound file differently in iOS 5.1.1, and I was completely out of luck. So far, I have tried to set the speed of 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]; 

no luck at all, the sound is reproduced, but simply ignores the speed that I give him. I also tried AVPlayer:

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

Again, he plays, but simply ignores the speed I set. I tried several different audio files, but for the sake of this topic, I chose Rooster-mono.wav from this directory: http://sig.sapp.org/sounds/wave/

Has anyone had success in changing the playback speed on iOS 5.1.1? Or does anyone know what I'm missing here?

I am doing this in order to slightly change the pitch of some of my samples, I understand that I can do this using RemoteIO or something like that, but this seems like a complete excess for what I'm trying to achieve (simple adjustment of playback speed).

+6
source share
4 answers

Here is the code that I know that works, I just tried it in the application I was working on. as you mentioned, using setEnableRate: and setRate: will only work with iOS 5.0 and above. so I use respondsToSelector: to check on the device whether 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; 

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

So the only way to get the right speed is to have iOS 5 on your device.

+8
source

Change the code to:

 avPlayer = [[AVPlayer alloc] initWithURL:referenceURL]; [avPlayer play]; //call play first avPlayer.rate = 0.5; //then set rate 
+8
source

This is how you do it.

speed 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]; 
+3
source

Swift 2.0

 let player = AVPlayer(URL: url) player.play() player.rate = 0.9 
+1
source

Source: https://habr.com/ru/post/918630/


All Articles