title: How to set iOS app to use usb audio for input and output to internal speakers
body: <p>I have 2 different makes of guitar adapters that connect to my iphone using the lightning connector</p>
  
  <p>When adapter 1 is plugged in, the device becomes a usb audio mic and it plays the sound through my iPhone's speakers as the adapter does not contain a headphone socket</p>
  
  <p>When adapter 2 is plugged in, the device becomes a usb audio mic but plays the sound through the headphone socket on the adapter.</p>
  
  <p>I'm trying to write an app that work with adapter 2, but rather than output the sound to the adapter's headphone socket, I want to route it through the iPhone's speakers.</p>
  
  <p>The code below should work, but what i'm finding is that calling <code>AVAudioSessionPortOverride</code> with the <code>AVAudioSessionPortOverrideSpeaker</code> option and the audio session’s category is <code>AVAudioSessionCategoryPlayAndRecord</code> causes audio to use the built-in speaker and microphone regardless of other settings, basically ignoring <code>setPreferredInput</code></p>
  
  <p>I can't quite understand how adapter 1 manages to take input from usb audio and output to speaker but my app can't because of the restrictions above. Anyone know of a solution?</p>
  
  <pre><code>AVAudioSession* session = [AVAudioSession sharedInstance];
  //Set the audioSession category. Needs to be Record or PlayAndRecord to use audioRouteOverride:
  [session    setCategory:AVAudioSessionCategoryPlayAndRecord 
              withOptions:AVAudioSessionCategoryOptionMixWithOthers 
              error:nil];
  
  //set the audioSession override
  [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
  error:nil];
  
  //activate the audio session
  [session setActive:YES error:nil];
  
  //set input to usb
  for (AVAudioSessionPortDescription *destPort in session.availableInputs){
      if ([destPort.portType isEqualToString:AVAudioSessionPortUSBAudio]) {
          [setPreferredInput:(AVAudioSessionPortDescription *)inPort
                  error:(nil)outError
                  session setPreferredInput:destPort error:nil];
      }
  }
  </code></pre>