How to force external microphone and phone speakers in Android?

So I have a code that takes an input from a microphone and simultaneously plays it through the speakers using the AudioRecorder

and classes AudioTrack

.

The code works fine, however what I would like to do is plug in headphones, use an external microphone as input and phone speakers as output.

I've already tried the class AudioManager

, but I can't seem to get it to work as intended.

By default, when headphones are plugged in, the external microphone is used as input and audio is output through the headphones ... However, when using the following code snippet:

audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

      

The input switches to the phone microphone, and the speakers also switch to the phone speakers.

I would like to continue to use my phone speakers but still use an external microphone. Is it possible?

I am compiling API 19 (KitKat) and I already have

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

      

in my manifest. Also the source is AudioRecorder

set asMediaRecorder.AudioSource.MIC

+3


source to share


1 answer


I had the same problem. After trying different combinations, I found a way to record from an external microphone and play back using the phone speaker. Here is the code that works for me with API level 25 and Asus Zenfone 3 ZE520KL (Android 6.0.1):

mAudioInput = new AudioRecord(MediaRecorder.AudioSource.MIC, mSampleRate, AudioFormat.CHANNEL_IN_MONO, mFormat, mInBufferSize);
mAudioOutput = new AudioTrack(AudioManager.STREAM_MUSIC, mSampleRate, AudioFormat.CHANNEL_OUT_MONO, mFormat, mOutBufferSize, AudioTrack.MODE_STREAM);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.setSpeakerphoneOn(true);

      



EDIT: it doesn't work with Samsung Galaxy Grand Prime (SM-G531Y)

0


source







All Articles