Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recording Audio from a Bluetooth Audio Device in Android

How can I record the voice from a paired Bluetooth audio device (i.e. Moster Clarity Bluetooth Speaker) in Android.

I've paired with the device from within Android, and I'd like to record the voice from the microphone on the device (as opposed to using the phone's built-in microphone).

Here's the code I'm using for recording:

try {
    isRecording = true;

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    if (file == null) {
        File rootDir = Environment.getExternalStorageDirectory();
        file = File.createTempFile(PREFIX, EXTENSION, rootDir);
    }

    recorder.setOutputFile(file.getAbsolutePath());
    recorder.prepare();
    recorder.start();

    timDown = new RecordCountDown(10000, 1000);
    timDown.start();

} catch (Exception e) {
    Log.i("Error Message", "Error Message :" + e.getMessage());
}

How can I do this?

like image 516
Prashant Avatar asked Jan 22 '13 09:01

Prashant


People also ask

Can you record using Bluetooth?

About this app Bluetooth Voice Recorder is an easy to use audio and voice recorder. This app allows user to record audio with the microphone on your phone, as well as a microphone on wired or Bluetooth headset. Please note that Bluetooth Voice Recorder is not designed to record phone calls.


2 Answers

Try this code maybe helpful for you..

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();
like image 98
RaviPatidar Avatar answered Nov 14 '22 22:11

RaviPatidar


code to voice recording from bluetooth headset

public class Recording {

static int count = 0;
static String Shared;
static String bFlag;
public static int TIMEOUT = 5000;
public static int COUNTDOWN_INTERVAL = 1000;
static Context context;

public static void checkAndRecord(Context context,
        OnBluetoothRecording BluetoothRecording, boolean resume) {

    // Check bluetooth flag And Bluetooth is ON or OFF
    if (getBluetoothFlag(context) && isBluetoothON()) {

        // Check for bluetooth and Record
        startBluetoothRecording(BluetoothRecording, resume, context);

    } else {

        // If Bluetooth is OFF Show Toast else Dont Show
        if (getBluetoothFlag(context) && !isBluetoothON()) {
            // false because recording not started
            Toast.makeText(context,
                    "Bluetooth is OFF. Recording from Phone MIC.",
                    Toast.LENGTH_SHORT).show();
            BluetoothRecording.onStartRecording(resume, false);
        } else {
            // false because recording not started
            BluetoothRecording.onStartRecording(resume, false);
        }
    }

}

private static void startBluetoothRecording(
        final OnBluetoothRecording BluetoothRecording,
        final boolean resume, Context context) {
    // TODO Auto-generated method stub

    final int MAX_ATTEPTS_TO_CONNECT = 3;
    final AudioManager audioManager = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);

    final CountDownTimer timer = getTimer(BluetoothRecording, audioManager,
            resume);

    context.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            int state = intent.getIntExtra(
                    AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
            if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
                // cancel Timer
                timer.cancel();
                context.unregisterReceiver(this);

                // pass through and true because
                // recording from bluetooth so set 8000kHz

                BluetoothRecording.onStartRecording(resume, true);

            } else if (AudioManager.SCO_AUDIO_STATE_DISCONNECTED == state) {
                if (count > MAX_ATTEPTS_TO_CONNECT) {
                    context.unregisterReceiver(this);
                    // Stop BluetoothSCO
                    audioManager.stopBluetoothSco();
                    // reset Counter
                    count = 0;
                    // stop timer
                    timer.cancel();
                    // false because still recording not started
                    BluetoothRecording.onStartRecording(resume, false);
                } else {
                    // Increment Disconnect state Count
                    count++;

                }
            }

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

    // Start the timer
    timer.start();
    audioManager.startBluetoothSco();

}

// set the Timeout
private static CountDownTimer getTimer(
        final OnBluetoothRecording BluetoothRecording,
        final AudioManager audioManager, final boolean resume) {
    // TODO Auto-generated method stub
    return new CountDownTimer(TIMEOUT, COUNTDOWN_INTERVAL) {

        @Override
        public void onTick(long millisUntilFinished) {
            // Do Nothing

        }

        @Override
        public void onFinish() {

            // stopBluetoothSCO() and start Normal Recording
            audioManager.stopBluetoothSco();

            // false because recording button is already clicked but still
            // not recording.
            BluetoothRecording.onStartRecording(resume, false);
        }
    };
}

// Return's the bluetooth state
private static boolean isBluetoothON() {

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    return bluetoothAdapter.isEnabled();
}

// Return's the bluetoothFlag state
private static boolean getBluetoothFlag(Context context) {

    // shared pref
    SharedPreferences sp = context.getSharedPreferences(Shared,
            Context.MODE_PRIVATE);
    return sp.getBoolean(bFlag, false);

}

}

Interface OnBluetoothRecording.java

public interface OnBluetoothRecording {

void onStartRecording(boolean state,boolean bluetoothFlag);
void onCancelRecording();
}
like image 33
Shivaraj Patil Avatar answered Nov 14 '22 22:11

Shivaraj Patil