android - 在 Android 中从蓝牙音频设备录制音频

标签 android bluetooth

如何在 Android 中从配对的蓝牙音频设备(即 Moster Clarity Bluetooth Speaker)录制语音。

我已经在 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());
}

我该怎么做?

最佳答案

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

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();

关于android - 在 Android 中从蓝牙音频设备录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14455797/

相关文章:

java - BLE GATT 服务器中的服务和配置文件有什么区别

javascript - 带有汉堡图标的响应式侧面导航菜单

android - 如何从 editText 解析两个 double ?

java - 如何使用java访问linux蓝牙堆栈

ios - 带有 iOS ExternalAccessoryFramework 的蓝牙经典

安卓蓝牙 : how to detect which slave is disconnected when there are multiple slaves?

java - Android蓝牙编程的代码示例

android - 错误 : incompatible types: View cannot be converted to TextView and Error:Execution failed for task ':app:compileDebugJavaWithJavac'

android - 添加一个按钮以选择多选微调器中的所有项目

java - 关键字 Implements 不工作 Java