禁用安卓手机内置麦克风

3

我正在开发一个使用语音识别的应用程序,我想在使用蓝牙耳机时禁用内部麦克风。问题是,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()


请点击以下链接:https://dev59.com/LWw15IYBdhLWcg3wCXOJ - Asad Ullah Rafiq
谢谢!看到了这篇帖子,但是它是2-3年前的,我认为有些问题已经解决了。 - cortex
1个回答

0

当可用时,Android 应该能够使用耳机麦克风进行管理,是否有效需要使用:

setAudioSource(AudioSource.DEFAULT);

更多信息在这里

希望这可以帮助。


但是我根本没有使用媒体记录器。 - cortex
那你用的是什么?我们需要更多信息来帮助你。 - Nanoc
更新了问题。谢谢!MediaRecorder与语音识别有什么关系?在启动识别引擎之前,我是否需要创建一个实例并设置音频源? - cortex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接