AVAudioPlayer quiet when changing songs on speaker - ios

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".

Related

AVAudioPlayer doesn't play sound when MPMusicPlayerController is playing on iOS

I have an app that plays various musical sounds using AVAudioPlayer. It lets people select a song from their library and plays it with MPMusicPlayerController. It used to work that they could jam with it, but now when MPMusicPlayerController is playing it doesn't play the sound using AVAudioPlayer. It even doesn't play the sound once MPMusicPlayerController has stopped. I've tested it on 13.4.1 and 13.6. What changed to make it stop working and what can I do to fix it?
It plays the sound like this:
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
[player prepareToPlay];
[player setVolume: 1.0];
[player play];
For the music, it gets a MPMediaItemCollection using MPMediaPickerController, then plays it like this:
- (void) playMusic: (MPMediaItemCollection *) collection {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
if (musicPlayer == nil) {
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[musicPlayer setShuffleMode: MPMusicShuffleModeOff];
[musicPlayer setRepeatMode: MPMusicRepeatModeNone];
}
[musicPlayer setQueueWithItemCollection:collection];
[musicPlayer play];
}
Feeling stupid. The phones were set to silent mode. For some reason, it can play the sounds or the music on silent, but it can't play them both together.

Play music in background in ios

I am streaming music file using AVAudioPlayer, It plays fine in background if i use nib file but when i try the same code using storyboard, The music playing stoped. I can't understand the reason why its stop playing in background when i using storyboard.
i am using this code for it
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:0.1];
[audioPlayer play];
Can any help me to play it in background using storyboard. Thanks in advance.
Let's try:
#implementation YourViewController
{
AVAudioPlayer *audioPlayer;
}
// if your are using ARC, after exiting this method, system will kill your audioPlayer. This is the reason why you can not play the music.
// in your method:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

MPMoviePlayerController unmutes hardware mute in AVAudioPlayer

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.

Play multiple sounds (one by one) with AVAudioPlayer

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.

Is it possible to change the audio play speed with AVAudioPlayer in iPhone?

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.

Resources