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/des
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);
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.