android - 禁用安卓内置麦克风

标签 android android-5.0-lollipop android-bluetooth

我正在开发一个使用语音识别的应用程序,我想在使用蓝牙耳机时禁用内部麦克风。问题是内置的 android 麦克风一直在听,识别引擎识别出我不想要的词(其他人在电话附近说话,或者环境噪音使识别有用)。谢谢!


public class BluetoothHelper extends BluetoothHeadsetUtils {

    private Context _mContext;
    private AudioManager audioManager;
    private int audioModeBackup;

    public BluetoothHelper(Context context) {
        super(context);
        this._mContext = context;
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public void onScoAudioDisconnected() {
        // Cancel speech recognizer if desired
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamSolo(AudioManager.USE_DEFAULT_STREAM_TYPE, true);


        Log.d(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall());
        Toast.makeText(_mContext, "SCO Audio disconnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScoAudioConnected() {
        // Should start speech recognition here if not already started
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
        Log.d(BluetoothHelper.class.getSimpleName(), "Is bluetooth sco on: "+audioManager.isBluetoothScoOn());

        Log.d(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall()+" SCO ON: ");
        Toast.makeText(_mContext, "SCO Audio connected.  Audio is on headset SCO: " + this.isOnHeadsetSco(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHeadsetDisconnected() {
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);

        Log.i(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                + audioManager.isBluetoothScoAvailableOffCall());
        Toast.makeText(_mContext, "Bluetooth Headset Off", Toast.LENGTH_SHORT).show();
        /* Unmute the external microphone */
        setInternalMicMute(false);
    }

    @Override
    public void onHeadsetConnected() {
        AudioManager audioManager = (AudioManager) _mContext.getSystemService(Context.AUDIO_SERVICE);

        if(!audioManager.isBluetoothA2dpOn()){
            Log.d("BluetoothHelper", "Reset connection with bluetooth");
            this.setStarted(false);
            this.start();
        }
        else{
            this.mIsCountDownOn = true;
            Log.i(BluetoothHelper.class.getSimpleName(), "A2DP: " + audioManager.isBluetoothA2dpOn() + ". SCO: "
                    + audioManager.isBluetoothScoAvailableOffCall());
            Toast.makeText(_mContext, "Bluetooth Headset On.", Toast.LENGTH_SHORT).show();
            /* Mute the external microphone */
            setInternalMicMute(true);
            this.mCountDown11.start();
        }

    }


    private void setInternalMicMute(boolean mute) {

    }
}

更新问题!我正在使用这些辅助类来检测蓝牙耳机何时连接,之后我从 BluetoothHeadset 类调用 startVoiceRecognition()

最佳答案

Android 应该设法在可用时使用头戴式麦克风是否可以使用:

setAudioSource(AudioSource.DEFAULT);

更多信息 here .

希望这对您有所帮助。

关于android - 禁用安卓内置麦克风,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821221/

相关文章:

android - 如何使用 Logcat 打印非常大的字符串

Android Material Design Inline Datepicker问题

java - 如何使用 AccessibilityService.getWindows() 获取可遍历的 AccessibilityNodeInfo?

android - 如何在 Android 中通过蓝牙流式传输来电音频

c# - Android(c2dm)推送通知错误

java - 需要帮助来发现java空指针异常

java - 如何修复 'mp4 video not running in Exoplayer 2.9'

Android 从路径加载库

java - 安卓-Arduino蓝牙通讯: app stops reading inputstream

Android通过蓝牙接收接收文本