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.
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 AVAudioPlayer playing a song through the speaker. I made the audio play through the speaker like this.
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
Now I have an AVAudioPlayer which I set up like this.
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
and I play it like this
[player prepareToPlay];
[player play];
Everything is fine until I change the audio player like this, where I first update currentSong to a new object and init from that
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
[player setVolume:1.0f];
[player prepareToPlay];
[player play];
Now the output is quiet, still on the speaker but quiet. If I change the audio output back to AVAudioSessionPortOverrideNone and the back to AVAudioSessionPortOverrideSpeaker, the sound is loud again, but switching the AVAudioPlayer sends me back to "quiet".
I have a working AudioServicesPlaySystemSound code to run a 10s background music.
Is there a way for me to loop this audio every 10s ? I want it to run forever until an action is triggered to stop the audio (e.g: a button is pressed to stop the loop)
I read an article about dispatch_after and thought that it might be an appropriate way to create the loop.
Switch to AVAudioPlayer instead. Not only can you specify a number of times to play the sound, but you have more control over the sound, including volume level.
It isn't that much harder. For example, here is how you would play a sound, looped indefinitely:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *player = [{AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
player.numberofLoops = -1;
[player play];
and this is how you would stop it:
[player stop];
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'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.