Here is native module to route audio input:
// 1 bluetooth
// 2 wired device
// 0 phone speaker
@ReactMethod
public void setAudioInput(String route) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
MediaRecorder recorder = new MediaRecorder();
if (route.contains("Bluetooth")) {
for (AudioDeviceInfo device : devices){
if (device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_SCO || device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP) {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setBluetoothScoOn(true);
audioManager.startBluetoothSco();
Log.d("setAudioRoute", "bluetooth");
break;
}
}
} else if (route.contains("Wired")) {
for (AudioDeviceInfo device : devices) {
if (device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
audioManager.setMicrophoneMute(false);
Log.d("setAudioRoute", "wired");
break;
}
}
} else {
for (AudioDeviceInfo device : devices) {
if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_MIC) {
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setMicrophoneMute(false);
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
Log.d("setAudioRoute", "Built-in mic");
break;
}
}
}
}
I test in android 12, 13(Samsung, realme) is good. But not work in android 11(Sharp, Vsmart, Oppo).
Here is logcat in android studio:
2023-03-21 14:13:12.465 30597-30780 org.webrtc.Logging com.walkietalkieapp I WebRtcAudioRecordExternal: Number of active recording sessions: 1
2023-03-21 14:13:12.465 30597-30780 org.webrtc.Logging com.walkietalkieapp I WebRtcAudioRecordExternal: AudioRecordingConfigurations:
2023-03-21 14:13:12.467 30597-30780 org.webrtc.Logging com.walkietalkieapp I WebRtcAudioRecordExternal: client audio source=VOICE_COMMUNICATION, client session id=1329 (1329)
Device AudioFormat: channel count=1, channel index mask=0, channel mask=IN_MONO, encoding=PCM_16BIT, sample rate=48000
Client AudioFormat: channel count=1, channel index mask=0, channel mask=IN_MONO, encoding=PCM_16BIT, sample rate=48000
AudioDevice: type=TYPE_WIRED_HEADSET, id=427
2023-03-21 14:13:12.467 30597-30780 org.webrtc.Logging com.walkietalkieapp I WebRtcAudioRecordExternal: verifyAudioConfig: PASS
Why AudioDevice: type=TYPE_WIRED_HEADSET but app still use built-in mic?
Thank you.
Vu.