如何使用蓝牙耳机进行录音如何使用、蓝牙耳机

2023-09-12 07:17:09 作者:走路太骚会闪腰

我写一个Android应用程序用于存储和管理语音备忘录一些基本的元数据和标签。当录音我使用:

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

这工作得很好以正常方式使用手机的时候。但是,它并没有检测到蓝牙耳机的presence,仍然使用手机自带的麦克风,即使当耳机插入。

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.

我也尝试过使用MediaRecorder.AudioSource.DEFAULT,希望它会自动选择正确的来源,但没有健全的记录都没有。

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

我怎样才能一)检测一个蓝牙耳机插入和/或b)使用蓝牙耳机作为媒体记录器音源?

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?

推荐答案

olivierg基本上是正确的(AudioSource尚可MIC),一些基本的code是这样的:

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

这我偶然发现了这个自己只是一遍,我想指出Slott酒店的评论的重要性,包括正确的权限,最重要的是设置

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.