I’ve been able to route my audio to the speakerPhone, however it still will not play at the maximum possible volume. I’ve tried the [audioAlert setVolume:1.0] and it still remains at the volume that the user currently has it set at. If anyone has an idea what I’m doing wrong I’d appreciate it so much.
Here’s my code:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert setVolume:1.0];
[audioAlert prepareToPlay];
[audioAlert play];
Thanks to PaulPerry!
NOW SOLVED:
I included a little extra to also set it back to the previous volume using the code below:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert prepareToPlay];
float maxVolume = 1.0;
float currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:maxVolume];
[audioAlert play];
//set up timer to wait until audioAlert is finished playing,
// and then call the line below to set the volume back to it's original setting
[[MPMusicPlayerController applicationMusicPlayer]setVolume:currentVolume];
This will reset the volume for the entire system to maximum.
float volume = 1.0;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];