title: Play the maximum speakerPhone volume and then reset to previous volume with AVAudioPlayer --- IOS
body: <p>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.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
  
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;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];
  </code></pre>
  
  <p>Thanks to PaulPerry!</p>
  
  <p>NOW SOLVED: </p>
  
  <p>I included a little extra to also set it back to the previous volume using the code below:</p>
  
  <pre><code> AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
  
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;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];
  </code></pre>