Samik
Samik

Reputation: 390

How to force external microphone and phone speakers in Android?

So I have a code which takes input from the microphone and simultaneously plays it back through the speakers using the AudioRecorder and AudioTrack classes.

The code works perfectly, however what I would like to do is plug in a earphone, use the external mic as input and phone speakers as output.

I have already tried AudioManager class, but I can't seem to get it to work as intended.

By default when the earphones are plugged in, the external mic is used as input and the sound comes out through the earphones ... However on using the following code snippet:

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

The input switches to phone mic and the speakers switch to phone speakers as well.

I would like to keep using the phone speakers but still use the external mic. Is this possible?

I am compiling on API level 19 (KitKat), and I already have

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

in my manifest. Also the AudioRecorder source is set as MediaRecorder.AudioSource.MIC

Upvotes: 4

Views: 3751

Answers (1)

cges30901
cges30901

Reputation: 540

I had the same problem. After trying out different combinations, I found the way to record from external microphone and play with 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)

Upvotes: 1

Related Questions