23

I am writing an android app for storing and managing voice memos with some basic metadata and tagging. When recording sound I use:

recorder = new MediaRecorder();         
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(currentRecordingFileName);
// and so on

This works well when using the phone in a normal fashion. However, it does not detect the presence of a bluetooth headset and still uses the phone's own microphone even when the headset is plugged in.

I also tried using MediaRecorder.AudioSource.DEFAULT, hoping it would automatically choose the correct source, but then no sound was recorded at all.

How can I a) detect if a bluetooth headset is plugged in and/or b) use a bluetooth headset as audio source for the media recorder?

3 Answers 3

14

olivierg is basically right (AudioSource can still be MIC), some basic code would look like this:

    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
            Log.d(TAG, "Audio SCO state: " + state);

            if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { 
                /* 
                 * Now the connection has been established to the bluetooth device. 
                 * Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
                 * new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 * AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
                 *
                 * After finishing, don't forget to unregister this receiver and
                 * to stop the bluetooth connection with am.stopBluetoothSco();
                 */
                unregisterReceiver(this);
            }

        }
    }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));

    Log.d(TAG, "starting bluetooth");
    am.startBluetoothSco();

This I stumbled upon this myself just again, I want to point out the importance of slott's comment to include the right permissions, most importantly to set

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

in your manifest file. Without it you will not get any error message but the state will simply not change to connected.

3
  • 5
    Dont forget to set the permissions: <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
    – slott
    Jul 30, 2012 at 20:29
  • @Stephan, I tried your method and it worked fine with Bluetooth handsfree speaker. But when I tried the same code in a car it didn't work, any idea if the protocol used in car is different than the handsfree speakers?
    – AndroidDev
    Feb 17, 2014 at 10:43
  • @Stephan: I tried your code with handset bluetooth but it only record from my MIC in phone. It does not work in MIC of handset speaker. I am using HS3000 speaker and galaxy S2. This is my code . Please check help me problem github.com/mjohn123/HeadsetRecorder
    – John
    Dec 20, 2014 at 16:22
5

According to the documentation you need to start a SCO audio connection with AudioManager.startBluetoothSco(), and then it seems like you need to use MediaRecorder.AudioSource.VOICE_CALL.

As far as I can see, you can't select a particular device and such. This is performed at system level, ie after the user pairs the headset with the phone.

EDIT:

As mentioned by Stefan, the AudioSource needs to be MIC.

VOICE_CALL doesn't seem to work.

2

You can detect connected bluetooth devices like this:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

However, I'm not sure how you make it record from the headset and not the regular MIC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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