How to mute the beep sound for SpeechRecognizer?

18,841

Solution 1

Aww I would comment but I don't have the rep. However I would like to help.

Have you seen this:

Continues Speech Recognition beep sound after Google Search update

Is it possible to turn off the silent mode programmatically in android?

Mute the global sound in Android

It seems to me that the code is different depending on the android version - as stated in the first link, Google switched the output of the 'beep' to the media stream.

I am guessing one of those questions will have the solution. If so please post what you have done, as you stated many people seem to be having the problem.

My guess would be:

//mute audio

AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
             amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
             amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
             amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
             amanager.setStreamMute(AudioManager.STREAM_RING, true);
             amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);


//unmute audio

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

             amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
             amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
             amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
             amanager.setStreamMute(AudioManager.STREAM_RING, false);
             amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);

I imagine this answer from Witness applications user will work. Credit goes to: @WitnessApplications

Also the scope of this would be before you startListening(i);

Solution 2

Even I find it annoying, especially when you have your app running in background.

Here is what how I worked around it in JAVA:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);           
audio.adjustVolume(AudioManager.ADJUST_MUTE,AudioManager.FLAG_SHOW_UI);

Now this mutes the active stream, and most of the times(99/100) it turns out to be the music stream. So basically it mutes it and then beep disappears.

In my case I have added an event to do this based on my mood so I keep switching between ADJUST_UNMUTE and ADJUST_MUTE time to time. So far it is working for me.

But even I am looking for a better solution where it mutes just the beep not the whole stream.

Hope this helps some one.

Edit: I found out a better way than this which always mutes the Music stream.

AudioManager audio = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 0,  AudioManager.FLAG_SHOW_UI);

And to unmute do:

audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);

Solution 3

With the solution above from Mark, you will have a crash with the following description:

Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65736, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity java.lang.SecurityException: Not allowed to change Do Not Disturb state

If you just want to mute the beep sound for speech recognizer, you just have to mute the AudioManager.STREAM_MUSIC.

In Kotlin, it looks like this for me:

object AudioUtils {

    @JvmStatic fun muteAudio(shouldMute: Boolean, context: Context)
    {
        val audioManager: AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val muteValue = if (shouldMute) AudioManager.ADJUST_MUTE else AudioManager.ADJUST_UNMUTE
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, muteValue, 0)
    }

}
Share:
18,841
portfoliobuilder
Author by

portfoliobuilder

I am a mobile app developer. Hit me up if you have any questions: [email protected]

Updated on July 23, 2022

Comments

  • portfoliobuilder
    portfoliobuilder almost 2 years

    This has been asked before, but no one seemed to have a solution: Muting SpeechRecognizer's beep sound

    Nevertheless, I still would like to know if anyone knows how to mute the beeping sound for SpeechRecognizer?

    I create speechRecognizer object: private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);

    And then in my class I instantiate the speechRecognizer like this

    sr.setRecognitionListener(new listener());
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);             
    i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
            .getClass().getName());
    i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
    i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 7000);
    sr.startListening(i);
    

    Anyone with any good ideas? I researched that I could create an object of AudioManager (AudioManager mAudioManager) and then using setStreamSolo(), I could mute the sound. But I am not sure how to implement this. I added it to my instantiation code for speechRecognizer and nothing happened. Is this something I should call from my main class?

    mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
    

    Thank you in advance.