Reputation: 7856
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.
Upvotes: 11
Views: 22471
Reputation: 51
I ended up using RecognitionListener events and setting ringer mode to vibrate.
val originalRingerMode = audioManager.ringerMode // Save the current ringer mode before events call.
val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context)
speechRecognizer.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(bundle: Bundle?) {
// Put the phone in vibrate mode temporarily to suppress default beep
if(originalRingerMode == AudioManager.RINGER_MODE_NORMAL)
audioManager.ringerMode = AudioManager.RINGER_MODE_VIBRATE
}
override fun onEndOfSpeech() {
if(originalRingerMode == AudioManager.RINGER_MODE_NORMAL) {
CoroutineScope(Dispatchers.Default).launch {
delay(800)
audioManager.ringerMode = originalRingerMode
}
}
}
override fun onError(errorCode: Int) {
if(originalRingerMode == AudioManager.RINGER_MODE_NORMAL) {
CoroutineScope(Dispatchers.Default).launch {
delay(800)
audioManager.ringerMode = originalRingerMode
}
}
}
}
Checking original ringerMode is required because if users is already in silent mode, then setting to vibrate mode will through DND error.
Upvotes: 0
Reputation: 33
For public interest, I want to post an updated to this thread. The year is 2023. I am unable to find any method yet added that will suppress this awful bleeping. However, it can be suppressed, as others have indicated by muting the notification stream. (Note that this appears to be different than mentioned in some of the older answers to this question.) I achieve the effect thus:
audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
// to mute:
priorStreamVolume = audioManager.getStreamVolume(
AudioManager.STREAM_NOTIFICATION
);
audioManager.setStreamVolume(
AudioManager.STREAM_NOTIFICATION,
0,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE
);
// to unmute:
audioManager.setStreamVolume(
AudioManager.STREAM_NOTIFICATION,
priorStreamVolume,
AudioManager.FLAG_ALLOW_RINGER_MODES
);
The flags I used may or may not be relevant to your own situation.
Upvotes: 3
Reputation: 403
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);
Upvotes: 3
Reputation: 967
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)
}
}
Upvotes: 2
Reputation: 491
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);
Upvotes: 20