Forcing iPhone Microphone as Audio Input

Force iPhone to output through the speaker, while recording from headphone mic

After talking with Apple engineers on the Coreaudio mailing list, I've found it's not currently possible to do this with a single RemoteIO audio unit.

I'm going to try to instantiate two audio units, one for playing, the other for acquisition, but I'm not hopeful. We shall see.

Choose between built in mic and headset in iOS

Warning: This answer applies to iOS6 only. It is not correct for iOS7 or later. See comments for details.

This is possible, but only with a side effect. Changing the input device also changes the output device. You will not be able to record through the internal mic and at the same time listend to your recording in realtime through the headset's headphones. That means that if you do not want to listen simultaneously to your recording through the headphones, there is a solution for you:

When having connected a headset (= combined headphones and mic), you can choose between two alternatives:

  • internal mic for input and speaker for output
  • headset's mic for input and headset's headphones for output

(You can not use a mixture of these.)

You choose one of the alternatives by setting the property kAudioSessionProperty_OverrideAudioRoute through the function AudioSessionSetProperty of the Audio Session Services API. The documentation of the property says:

If a headset is plugged in at the time you set this property’s value
to kAudioSessionOverrideAudioRoute_Speaker, the system changes the
audio routing for input as well as for output: input comes from the
built-in microphone; output goes to the built-in speaker.

AVAudioSession : microphone headphone as input and iphone speaker as output

How I undeerstand this Apple documentation, this is not possible using AVAudioSession:

If a headset is plugged in at the time you set this property’s value to kAudioSessionOverrideAudioRoute_Speaker, the system changes the audio routing for input as well as for output: input comes from the built-in microphone; output goes to the built-in speaker.

How to select external microphone

Using AVAudioSession, get the availableInputs. The return value is an array of AVAudioSessionPortDescriptions. Iterate through the array checking the portType property to match your preferred port type, then set the preferredInput using the port description.

Swift:

let audioSession = AVAudioSession.sharedInstance()
if let desc = audioSession.availableInputs?.first(where: { (desc) -> Bool in
return desc.portType == AVAudioSessionPortUSBAudio
}){
do{
try audioSession.setPreferredInput(desc)
} catch let error{
print(error)
}
}

Objective-C:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSString *preferredPortType = AVAudioSessionPortUSBAudio;
for (AVAudioSessionPortDescription *desc in audioSession.availableInputs) {
if ([desc.portType isEqualToString: preferredPortType]) {
[audioSession setPreferredInput:desc error:nil];
}
}

iOS: How to choose which microphone (inbuilt/external) to use?

You have to use the AUHAL unit to set a specific input device as default input and then connect it with the AudioQueue.

Apple has a detailled Technical Note for that: Device input using the HAL Output Audio Unit

Override to use a device's internal microphone only

Excerpted from Apple Docs:

Sets the preferred input port for audio routing.

func setPreferredInput(_ inPort: AVAudioSessionPortDescription?) throws

Setting the preferred input port requests a change to the input audio route. To determine whether the change takes effect, use the
currentRoute property. The value of the inPort parameter must be one of the AVAudioSessionPortDescription objects in the
availableInputs array. If this parameter specifies a port that is not already part of the current audio route and the app’s session controls audio routing, this method initiates a route change to use the preferred port.You must set a preferred input port only after setting the audio session’s category and mode and activating the session.



Related Topics



Leave a reply



Submit