Disable 'Ready' sound of recognition listener

后端 未结 2 906
旧时难觅i
旧时难觅i 2021-02-15 04:17

I implement a RecognitionListener inside of a tabbed fragment activity. What happens is that as the user scrolls around, the fragment that implements the listener is created/des

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 04:52

    As zapl said that there is no direct method to do that but you can try muting the system volume and as you, Tencent said that you are avoiding any kind of hack. Then I must say there is no method SO FAR to do this, But still I'm sharing this trick solution for those who are suffering from same issue and may not mind a "hack" trick. Just saving their hours of frustration.

    Mute the system volume for that particular time then un-mute after. How to do this:

    In your class, create following objects

    private AudioManager mAudioManager;
    private int mStreamVolume = 0; 
    

    In your onCreate method do following.

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    

    When you start listening for Speech Recognition, do following

    speechRecognizer.startListening(intent); // this method make that annoying sound
    mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // getting system volume into var for later un-muting 
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // setting system volume to zero, muting
    

    In your Speech RecognitionListener interface there should be a method onReadyForSpeech.

    class MyRecognitionListener implements RecognitionListener          
    {
      public void onReadyForSpeech(Bundle params)
      {
        // this methods called when Speech Recognition is ready
        // also this is the right time to un-mute system volume because the annoying sound played already
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamVolume, 0); // again setting the system volume back to the original, un-mutting
      }
    }
    

    This is how you can play a trick with this annoying sound. Obviously any other sound will also be mute for a second.

提交回复
热议问题