Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable 'Ready' sound of recognition listener

Tags:

android

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.

like image 280
ian.shaun.thomas Avatar asked Aug 14 '12 11:08

ian.shaun.thomas


2 Answers

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);

like image 157
dalvallana Avatar answered Oct 17 '22 16:10

dalvallana


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.

like image 38
Bilal Rabbani Avatar answered Oct 17 '22 15:10

Bilal Rabbani