Home » Android » android – Disable 'Ready' sound of recognition listener

android – Disable 'Ready' sound of recognition listener

Posted by: admin June 15, 2020 Leave a comment

Questions:

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/destroyed and on my Nexus 7 and Galaxy Nexus you hear a sound associated to the RegonitionListener, or maybe its the SpeechRecognizer, being ready for use. Whichever class is the cause of the sound, I would like to allow the user to disable it. I realize currently my issue is that I am listening without the users focus which I am resolving; however, I still do not want to hear the sound by default and prefer to let the user opt into the notification sound.

So my question is this, is it possible to disable the notification sound associated to the listener? I have been unable to find this in the docs.

How to&Answers:

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.

Answer:

Previous answer is OK except it will crash because of a NullPointer Exception (getStreamVolume is being called on a null object). To fix that, before the getStreamVolume line add:

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

Answer:

If you want to stop the end sound as well, you can unmute the sound in onResults and onError instead of onReadyForSpeech.

Create a handler as a member variable:

private Handler mHandler = new Handler();

The RecognitionListener methods:

@Override
public void onResults(Bundle results) {
    // Your logic here
    startAudioSound();
}

@Override
public void onError(int errorCode) {
    // Your logic here
    startAudioSound();
}

private void startAudioSound(long delay) {
    mHandler.postDelayed(() -> {
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mStreamVolume, 0); // again setting the system volume back to the original, un-mutting
    }, 300);
}

I’ve tested with a 300 millisecond delay and it works. Shorter delays may also work.