title: Play audio through upper (phone call) speaker
body: <p>I'm trying to get audio in my app to play through the upper speaker on the iPhone, the one you press to your ear during a phone call.  I know it's possible, because I've played a game from the App Store ("The Heist" by "tap tap tap") that simulates phone calls and does exactly that.</p>
  
  <p>I've done a lot of research online, but I'm having a surprisingly hard time finding ANYONE who has even discussed the possibility.  The overwhelming majority of posts seem to be about the handsfree speaker vs plugged-in earphones, (like <a href="https://stackoverflow.com/questions/15128455/switching-between-headphone-speaker-on-iphone?rq=1">this</a> and <a href="https://stackoverflow.com/questions/5931799/redirecting-audio-output-to-phone-speaker-and-mic-input-to-headphones?rq=1">this</a> and <a href="https://stackoverflow.com/questions/9963011/in-ios-5-how-to-use-audiosession-to-make-sure-audio-is-played-through-speaker-a?rq=1">this</a>), rather than the upper "phone call" speaker vs the handsfree speaker.  (Part of that problem might be not having a good name for it: "phone speaker" often means the handsfree speaker at the bottom of the device, etc, so it's hard to do a really well-targeted search).  I've looked into Apple's <code>Audio Session Category Route Overrides</code>, but those again seem to (correct me if I'm wrong) deal only with the handsfree speaker at the bottom, not the speaker at the top of the phone.</p>
  
  <p>I have found ONE post that seems to be about this: <a href="http://iphonedevsdk.com/forum/iphone-sdk-development/107557-sound-play-through-upper-speaker.html" rel="nofollow noreferrer">link</a>.  It even provides a bunch of code, so I thought I was home free, but now I can't seem to get the code to work.  For simplicity I just copied the <code>DisableSpeakerPhone</code> method (which if I understand it correctly should be the one to re-route audio to the upper speaker) into my <code>viewDidLoad</code> to see if it would work, but the first "assert" line fails, and the audio continues to play out the bottom.  (I also imported the AudioToolbox Framework, as suggested in the comment, so that isn't the problem.)</p>
  
  <p>Here is the main block of code I'm working with (this is what I copied into my <code>viewDidLoad</code> to test), although there are a few more methods in the article I linked to:</p>
  
  <pre><code>void DisableSpeakerPhone () {
      UInt32 dataSize = sizeof(CFStringRef);
      CFStringRef currentRoute = NULL;
      OSStatus result = noErr;
  
      AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &amp;dataSize, &amp;currentRoute);
  
      // Set the category to use the speakers and microphone.
      UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
      result = AudioSessionSetProperty (
                                        kAudioSessionProperty_AudioCategory,
                                        sizeof (sessionCategory),
                                        &amp;sessionCategory
                                        );
      assert(result == kAudioSessionNoError);
  
      Float64 sampleRate = 44100.0;
      dataSize = sizeof(sampleRate);
      result = AudioSessionSetProperty (
                                        kAudioSessionProperty_PreferredHardwareSampleRate,
                                        dataSize,
                                        &amp;sampleRate
                                        );
      assert(result == kAudioSessionNoError);
  
      // Default to speakerphone if a headset isn't plugged in.
      // Overriding the output audio route
  
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None; 
      dataSize = sizeof(audioRouteOverride);
      AudioSessionSetProperty(
                              kAudioSessionProperty_OverrideAudioRoute,
                              dataSize,
                              &amp;audioRouteOverride);
  
      assert(result == kAudioSessionNoError);
  
      AudioSessionSetActive(YES);
  } 
  </code></pre>
  
  <p><strong>So my question is this:</strong> can anyone either A) help me figure out why that code doesn't work, or B) offer a better suggestion for being able to press a button and route the audio up to the upper speaker?</p>
  
  <p>PS  I am getting more and more familiar with iOS programming, but this is my first foray into the world of AudioSessions and such, so details and code samples are much appreciated!  Thank you for your help!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>From the suggestion of "He Was" (below) I've removed the code quoted above and replaced it with:</p>
  
  <pre><code>[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error:nil];
  </code></pre>
  
  <p>at the beginning of <code>viewDidLoad</code>.  It still isn't working, though, (by which I mean the audio is still coming out of the speaker at the bottom of the phone instead of the receiver at the top).  Apparently the default behavior should be for <code>AVAudioSessionCategoryPlayAndRecord</code> to send audio out of the receiver on its own, so something is still wrong.</p>
  
  <p>More specifically what I'm doing with this code is playing audio through the iPod Music Player (initialized right after the AVAudioSession lines above in <code>viewDidLoad</code>, for what it's worth):</p>
  
  <pre><code>_musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
  </code></pre>
  
  <p>and the media for that iPod Music Player is chosen through an <code>MPMediaPickerController</code>:</p>
  
  <pre><code>- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
      if (mediaItemCollection) {
          [_musicPlayer setQueueWithItemCollection: mediaItemCollection];
          [_musicPlayer play];
      }
  
      [self dismissViewControllerAnimated:YES completion:nil];
  }
  </code></pre>
  
  <p>This all seems fairly straightforward to me, I've got no errors or warnings, and I know that the Media Picker and Music Player are working correctly because the correct songs start playing, it's just out of the wrong speaker. Could there be a "play media using this AudioSession" method or something?  Or is there a way to check what audio session category is currently active, to confirm that nothing could have switched it back or something?  Is there a way to emphatically tell the code to USE the receiver, rather than relying on the default to do so?  I feel like I'm on the one-yard line, I just need to cross that final bit...</p>
  
  <p><strong>EDIT:</strong> I just thought of a theory, wherein it's something about the iPod Music Player that doesn't want to play out of the receiver.  My reasoning: it is possible to set a song to start playing through the official iPod app and then seamlessly adjust it (pause, skip, etc) through the app I'm developing.  The continuous playback from one app to the next made me think that maybe the iPod Music Player has its own audio route settings, or maybe it doesn't stop to check the settings in the new app?  Does anyone who knows what they're talking about think it could it be something like that?</p>