从Android中的蓝牙音频设备录制音频

Pra*_*ant 7 android bluetooth

如何在Android中录制配对蓝牙音频设备(即Moster Clarity蓝牙音箱)的语音.

我已经在Android中与该设备配对,我想从设备上的麦克风录制语音(而不是使用手机的内置麦克风).

这是我用于录制的代码:

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());
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Rav*_*dar 8

试试这段代码可能对你有帮助..

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();
Run Code Online (Sandbox Code Playgroud)

  • 您是否会考虑添加一些叙述来解释为什么这段代码有效,以及是什么使它成为问题的答案?这对提出问题的人以及其他任何人来说非常有帮助. (4认同)