【问题标题】:How to record sound using bluetooth headset如何使用蓝牙耳机录制声音
【发布时间】:2011-04-30 20:56:58
【问题描述】:

我正在编写一个 android 应用程序,用于存储和管理带有一些基本元数据和标记的语音备忘录。录制声音时我使用:

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

这在以正常方式使用手机时效果很好。但是,它不会检测到蓝牙耳机的存在,并且即使在耳机插入时仍然使用手机自带的麦克风。

我也尝试过使用 MediaRecorder.AudioSource.DEFAULT,希望它会自动选择正确的源,但结果根本没有录制任何声音。

我如何 a) 检测是否插入了蓝牙耳机和/或 b) 使用蓝牙耳机作为媒体记录器的音频源?

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以这样detect connected bluetooth devices

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

    但是,我不确定您是如何通过耳机而不是常规 MIC 进行录音的

    【讨论】:

      【解决方案2】:

      根据文档,您需要使用AudioManager.startBluetoothSco() 启动SCO 音频连接,然后您似乎需要使用MediaRecorder.AudioSource.VOICE_CALL

      据我所知,您无法选择特定设备等。这是在系统级别执行的,即在用户将耳机与手机配对之后。

      编辑:

      正如 Stefan 所说,AudioSource 必须是 MIC。

      VOICE_CALL 似乎不起作用。

      【讨论】:

        【解决方案3】:

        olivierg 基本上是对的(AudioSource 仍然可以是 MIC),一些基本代码如下所示:

            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 的评论包含正确权限的重要性,最重要的是设置

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

        在您的清单文件中。没有它,您将不会收到任何错误消息,但状态不会更改为已连接。

        【讨论】:

        • 别忘了设置权限:
        • @Stephan,我试过你的方法,它与蓝牙免提扬声器配合得很好。但是当我在汽车上尝试相同的代码时它不起作用,知道汽车中使用的协议是否与免提扬声器不同吗?
        • @Stephan:我用手机蓝牙尝试了你的代码,但它只记录了我手机中的 MIC。它在手机扬声器的 MIC 中不起作用。我正在使用 HS3000 扬声器和 Galaxy S2。这是我的代码。请检查帮助我的问题github.com/mjohn123/HeadsetRecorder
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-25
        • 2020-11-03
        • 2020-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多