在android中连接蓝牙耳机后,如何在内置麦克风和耳机麦克风之间切换音频输入?

时间:2017-03-14 09:30:47

标签: android audio bluetooth headset

我正在使用语音识别应用。蓝牙耳机成功连接到我的Android设备后,我想在内置麦克风和耳机麦克风之间自由切换音频输入,怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码切换蓝牙耳机麦克风。

AudioManager audiomanager= (AudioManager)mContext.getSystemService(Context
            .AUDIO_SERVICE);
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    mContext.registerReceiver(mBluetoothScoReceiver, intentFilter);
audiomanager.startBluetoothSco();

并收听广播接收器。

private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);

        if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
            // Start recording audio
            log("audio connected");
            startRecording();
        } else if(state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){
            log("audio disconnected");
        }
    }
};

和切换(断开蓝牙耳机麦克风),您可以使用此代码。

mAudioManager.stopBluetoothSco();