Mute Recognition Listener "Ready" Sound

I am implementing a RecognitionListener inside a tab operation. It happens that as the user scrolls, the fragment that implements the listener is created / destroyed, and on my Nexus 7 and Galaxy Nexus you hear the sound associated with the RegonitionListener, or, possibly, its SpeechRecognizer , when you are ready to use it. Whatever class causes the sound, I would like to allow the user to disable it. Currently, I understand that the problem is that I am listening without focus to the users that I solve; however, as before, I do not want to hear the default sound and prefer the user to choose the notification sound.

So my question is, is it possible to mute the notification sound associated with the listener? I could not find this in the docs.

+7
source share
3 answers

As zapl said, there is no direct method to do this, but you can try to mute the system volume, and, like you, Tencent said that you avoid any hacking. Then I have to say that for this there is no SO FAR method. But still, I share this solution for those who suffer from the same problem, and perhaps not against the β€œtrick”. Just saving your hours of frustration.

Unmount the system volume for that particular time, and then mute the sound. How to do it:

In your class, create the following objects

private AudioManager mAudioManager; private int mStreamVolume = 0; 

In your onCreate mode, follow these steps:

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

When you start listening to speech recognition, follow these steps:

 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 

The SpeRec RecognitionListener interface must have an onReadyForSpeech method.

 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 } } 

Here's how you can play the trick with this annoying sound. Obviously, any other sound will also be muted within a second.

+14
source

The previous answer is fine, except that it crashes due to a NullPointer exception ( getStreamVolume is called on a null object). To fix this, before the getStreamVolume line add:

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

+3
source

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

Create the handler as a member variable:

 private Handler mHandler = new Handler(); 

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 tested with a delay of 300 milliseconds and it works. Shorter delays may also work.

0
source

All Articles