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).
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.
Modify the code to:
avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
[avPlayer play]; //call play first
avPlayer.rate = 0.5; //then set rate
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];
Swift 2.0
let player = AVPlayer(URL: url)
player.play()
player.rate = 0.9
Related
For example, play a local audio file with double speed or half speed.
I've already checked several Github projects, but none of them supports that.
Any idea about how to make it will be appreciated.
player.rate = 2.0 // Whatever speed you want
Assuming you're using AVAudioPlayer. you can change speed by changing the rate.
detail doc: https://developer.apple.com/documentation/avfoundation/avaudioplayer#jumpTo_15
Now it is possible to change the speed of sound.
here is my sample code:
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];
you set "enableRate" to YES and you can change it.
I have an 8 second mp3 file and I'm trying to loop it as main menu music. I do this by using SKAction repeatActionForever, however every time it starts over there is a small pause between the loops. This is very annoying since it doesn't sound like it is one long song this way. How can I fix this?
EDIT:
It also doesn't work with AVAudioPlayer :(
Use AVAudioPlayer and set the numberOfLoops property to -1 in order to loop the sound indefinitely.
For example:
NSError *error;
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"SomeSound.wav" withExtension:nil];
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
myPlayer.numberOfLoops = -1;
myPlayer.volume = 0.4;
myPlayer.delegate = self;
[myPlayer prepareToPlay];
[myPlayer play];
I've set my phone to muted state by switching vibrate/sound hardware button. In whole application some background music is playing unless the device is in muted state. So in this case, my music is playing but muted. Now in some point I want to present video (which is without sound but this shouldn't matter), however the video in MPMoviePlayerController turns music from AVAudioPlayer on even though the device is is 'muted' state. The AVAudioPlayer continues to play loudly even when i quit MPMoviePlayerController.
Some code samples, but i doubt it will help:
// movie without sound
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"]];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[_scrollView addSubview:_moviePlayerController.view];
[_moviePlayerController prepareToPlay];
[_moviePlayerController play];
// music
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
To fix this, I had to rewrite the code but instead of using MPMoviePlayerController I've used AVPlayer. Everything works like a charm now.
I am using AVAudioPlayer to make an MP3 player. I have multiple MP3 sounds and would like to play these sounds one by one. Following is the logic of my app:
///// For playing 1st sound
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
///// For playing 2nd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
///// For playing 3rd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:nil];
[mp3Player prepareToPlay];
[mp3Player play];
etc...
Just would like to know: I nil out the player and alloc AVAudioPlayer before playing the next sound. Is there any performance (speed) or memory (leak) concern in this logic? (I am using ARC). If so, is there any way to alloc AVAudioPlayer just ONE time, and to accept diffrent URLs pointing to those sounds? Thanks a lot in advance.
I think below link may help you:
How to play multiple audio files in sequence by loop in AVAudioPlayer?
In which audioPlayerDidFinishPlaying is used to play next song after one finishes.
I'm playing a local mp3 file with class 'AVAudioPlayer' on my iOS App.
And I want to change the speed when play it. such as I want to play this mp3 file slower.
How to achieve it?
Now it is possible to change the speed of sound.
here is my sample code:
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];
you set "enableRate" to YES and you can change it.
see more docs
That's not possible with AVAudioPlayer. See AVAudioPlayer: How to Change the Playback Speed of Audio? for potential workarounds.