2015-09-28 192 views
2

我正在开发一款使用语音识别的应用程序,并且我想在使用蓝牙耳机时禁用内置麦克风。问题是内置的安卓麦克风不停地收听,识别引擎识别出我不想要的单词(其他人在电话旁边说话,或者环境噪音使识别有用)。谢谢!禁用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()

+0

请点击链接http://stackoverflow.com/questions/6874881/how-does-setmicrophonemute-work –

+0

谢谢!看到这篇文章,但它是从2 - 3年前,我认为有些东西是固定的。 – cortex

回答

0

的Android应该管理使用耳机麦克风时可用它的工作原理使用:

setAudioSource(AudioSource.DEFAULT); 

更多信息here

希望这会有所帮助。

+0

但是我根本没有使用媒体记录器 – cortex

+0

然后你在用什么,我们需要更多的信息来帮助你。 – Nanoc

+0

更新了问题。谢谢 ! MediaRecorder与语音识别有什么关系?在启动识别引擎之前,我是否必须创建实例并设置音频源? – cortex