Stopping beep sound of google speech recognizer - android

I am using the default speech recognizer of Android. But you may know that it plays a beep sound when starting the recognizer and when the onError() event is fired. Beep sound is played from Android version 5.x.
My requirement is to implement continuous recognizer. So I have to start this recognizer again and again. In this situation beep sound is being played again and again. I have tried to have some solution like below:
Mute the media sound before starting recognizer and unmute again onSpeechReady() event. On the other hand, if recognizer do not detect any speech for 4/5 sec then it automatically calls onError() event and this onError() event plays the beep again. To resolve this, I have stopped the recognizer forcefully after 3 sec and called onError() by a timer before system calls this onError() event.
But as I closed the recognizer forcefully, system is getting an exception here and playing a buzz.
Anybody can give any solution on how I can stop this beep sound as well as the buzz.
Thanks in advance.

Use the following code before starting your speech
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);
Use the following code after getting result
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);

Related

MediaSession onMediaButtonEvent works for a few seconds then quits - Android

I've been trying to listen to media button intents on this Bluetooth button I bought off eBay for the past week and cannot figure out how to do it practically and reliably on my Oreo device. The first thing I tried was overriding onKeyDown in my main activity, which worked perfectly every time. However, I found it was impossible to listen to these calls once the application is minimized.
Next I tried a very hacky solution of reading the logcat line by line and checking if dispatched media key KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_NEXT was present in that line. It worked alright until I again exited the app, which then it would completely stop.
Finally I tried using a MediaSession and the onMediaButtonEvent method, which works perfectly for a few seconds to minutes after I start my activity then it just completely ceases to receive these intents.
Here's my code right now (I copied another answer on StackOverflow who included playing a dummy audio clip to try and get focus):
MediaSession ms = new MediaSession(getApplicationContext(), getPackageName());
ms.setActive(true);
ms.setCallback(new MediaSession.Callback() {
#Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyEvent.getKeyCode()) {
//do button specific stuff here
}
}
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
ms.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
at.play();
// a little sleep
at.stop();
at.release();
I'm just trying to figure out now, why it seems to work for a few seconds perfectly, then it just stops out of nowhere. I don't have any other media app like Spotify open or playing whatsoever, and if that was the issue, I've tried running the audio code every 5 seconds or so which still has no effect. The only thing that will reset it is fully restarting the app.
This code is in a service I have created. It only stops working if I am not in the app and simply press the home button to minimize it after a few seconds. I have a loop running to tell me that the service is still running, and it's seems it's running when it quits receiving the intents.
Any help would be appreciated.
I've tested this code, it seems the bluetooth button press is only listened to by your app if music (or silent sound) is currently playing. Otherwise the button press is not detected.

How to know if the media player is opening in background?

I am using Android 5.0 and 6.0. I want to check my default media player (calls from intent) is running/playing in background. Currently, I used the function
AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
if(mAudioManager.isMusicActive()){
Log.d("TAG","Running");
}
else{
Log.d("TAG","Not running");
}
However, it cannot know if the media player is in background but it is in stop/pause mode (looks like when you scroll down the notification bar and click the pause button in the media player). Do we have any function which can determine the media player is opened in background, but it is in stop/pause mode?

Muting SpeechRecognizer's beep sound

I'm using SpeechRecognizer API for my app, and everytime it starts, it plays "beep" sound.
I'd like to know how to mute it, So I could implement one of my own.
Thanks.
If you are using a button to activate and deactivate the recognizer you can mute sound onclick.
This doesnt work fantastically if you have it listening constantly, however for button clicks it should be fine :)
private AudioManager manager;
manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (isChecked)
{
manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
speech.startListening(recognizerIntent);
}
else
{
manager.setStreamMute(AudioManager.STREAM_MUSIC, false);
speech.stopListening();
speech.cancel();
}
Hope this helps (sorry if this is abit of a necrothread)

Adding mute button to application in android?

I my game application am using musics. I need mute and unmute button to put mob i silent mode.
Music runs in every activity like playing game and checking score.But mute button is added in menu activity.
I googled didn't get any exact result.
Use audio manager and set volume
AudioManager audioManager = (AudioManager) getSystemService(ctx.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
I'd choose to set the volume to 0 (mute) or 1 (unmute) with MediaPlayer.setVolume(float volumeLeft, float volumeRight). Also preserves the users volume settings. Trigger by Button is easily achieved with onClickListener().

How to check something at regular intervals?

Beginner here, I have a simple question.
In Android what would be the best what to check for something at regular intervals?
Please bear with me, I'll try to explain the best I can --
For example my audio app is very simple, a main activity and a service. The main activity has a UI with two buttons, start and stop audio. I press start and the audio service starts. Likewise when I click Stop the service stops and the audio ends. If isLooping() is hard-coded to true there is no issue because the audio never ends unless I hit stop button, which stops the audio service and also resets the button states.
This is an issue now because I set isLooping() to false so the audio doesn't loop. So the audio will stop playing but the service is still running.
I want to be able to detect when the audio stops so I can set the states of the UI buttons. So I need something that is always checking whether audio is playing (i.e. check player.isPlaying() so I can end the service and set the enable/disable state of the buttons.
I figured out binding to the service so I can access the MediaPlayer controls via my main activity so I know the code to check if it's playing, but WHERE do I put this code so it's checked all the time?
Am I making sense? I know this is probably very simple. Thanks for any help.
You can repeat it with the TimerTask and Timer. Code below:
public final void RepeatSoundFunction(){
t = new Timer();
tt = new TimerTask() {
#Override
public void run() {
mp.seekTo(0); //Reset sound to beginning position
mp.start(); //Start the sound
t.purge(); //Purge the sound
}
};
t.schedule(tt, 10*1000); //Schedule to run tt (TimerTask) again after 10 seconds
}
then you set a MediaPlayer onCompletionListener and in there you put this.
Inside the run-code you can check for other things than
music, I just show an example with the audio.

Categories

Resources