I'm using something like this to initiate audio recording:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/dev/null");
recorder.prepare();
recorder.start();
I want to always record from the internal microphone. Using the code above, the audio source switches to the external microphone as soon as one is plugged in. I've looked over the docs and can't find a way to set a preferred mic.
Unfortunately, you're trying to go against Android's audio management, this is, the system selects input device depending on selected audio source and, when the headset is pluged in, input, for MIC audio source, is switched to the headset. You can confirm this checking logcat's AudioPolicyManager's related messages.
My suggestion would be playing with different audio sources to trick the system. I would recommend using CAMCORDER or, at least, try VOICE_RECOGNITION or VOICE_COMMUNICATION.
Main problem with CAMCORDER is that it can be using multimedia microphone, this is, the one at the back of the device. More in detail, I would suggest following procedure:
- Listen for ACTION_HEADSET_PLUG Intent.
- When you receive the intent, try to switch audio source to CAMCORDER.
- CAMCORDER audio source is selected depending on active camera so you can try to force front camera usage (and, then, front internal mic) through MediaREcorder's setCamera
Hope this helps