0

Problem

I am unable to get Android's MediaRecorder to record audio from a connected Bluetooth microphone. Instead audio recordings use the device's inbuilt microphone

Question

Has anyone found a reliable way to get Android's MediaRecorder to record audio input from a connected Bluetooth microphone?

Research

Earlier answers suggest using the now deprecated startBluetoothSco(). Without startBluetoothSco() there appears to be no way to "encourage" Android to use a connected Bluetooth microphone over device microphone(s).

The following code successfully records audio, but only from the device microphone and never from the Bluetooth microphone

mediaRecorder = MediaRecorder(this).apply {
    setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION) // or .MIC
    setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP) // or MPEG_4
    setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
    setAudioChannels(1) // Mono
    setAudioSamplingRate(16000)
    setOutputFile(audioOutputFile)

    // use 
    prepare()
    start()

Earlier experience

When startBluetoothSco() was depreciated in my Text-to-Speech app I simply replaced startBluetoothSco() with a suitable audio mode (AudioManager.STREAM_MUSIC) and Android routed audio to the speaker.

Below is the code I have used to try a similar trick. It lists connected Bluetooth devices and if they appear to be the desired microphone change the audio mode:

val bluetoothA2DPConnectedDevices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS).filter { it.type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP }
bluetoothA2DPConnectedDevices.forEach { bluetoothDevice ->
    audioManager.mode = AudioManager.MODE_IN_CALL // if find a suitable Bluetooth device try changing audio mode
}

I have tried multiple AudioManager modes but none resulted in audio recordings using the external microphone

Can anyone suggest what else I could try ?

1 Answer 1

0

The solution I landed on uses setCommunicationsDevice() BEFORE creating mediaRecorder = MediaRecorder(this).apply {}

Search for, and if found select, any suitable Bluetooth connected external microphone

val availableCommunicationsDevices = audioManager.availableCommunicationDevices
availableCommunicationsDevices.forEach { device ->
    if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP) {
            audioManager.setCommunicationDevice(device)
    }
}

Then use the code shown in the original questoin to set up the MediaRecorder

Note: While MediaRecorder has a method setPreferredDevice() I was unable to get MediaRecorder to use the device.

Not the answer you're looking for? Browse other questions tagged or ask your own question.