3

I am using The Android Speech Recognition Service, to listen to the users speech. The service constantly runs in the background, and as soon as it gets a signal (e.g. a button press), I send a message to the service, which opens the microphone and the service starts listening for the next 5 seconds.

protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));

After that it stops listening, until it gets the signal again. It is basically the same code as here and it works very well.

But I would like to add this "Beep"-Sound when the microphone opens. I thought the speech recognition class has this beep automatically? Therefore I didn't want to load an extra "beep"-mp3 but use the built in function.

Can you tell me, how to activate the "beep"-sound?

public class VoiceCommandService extends Service {

    protected AudioManager mAudioManager;
    protected SpeechRecognizer mSpeechRecognizer;
    protected Intent mSpeechRecognizerIntent;
    protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));

    protected boolean mIsListening;
    protected volatile boolean mIsCountDownOn;

    static final int MSG_RECOGNIZER_START_LISTENING = 1;
    static final int MSG_RECOGNIZER_CANCEL = 2;
    protected final String LOG_TAG = VoiceCommandService.class.getSimpleName();


    @Override
    public IBinder onBind(Intent intent) {
        Log.d(LOG_TAG, "onBind");
        return mServerMessenger.getBinder();
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, maxVolume, AudioManager.FLAG_SHOW_UI);

        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                this.getPackageName());

    }


    protected static class IncomingHandler extends Handler {
        private WeakReference<VoiceCommandService> mtarget;

        IncomingHandler(VoiceCommandService target) {
            mtarget = new WeakReference<VoiceCommandService>(target);
        }


        @Override
        public void handleMessage(Message msg) {
            final VoiceCommandService target = mtarget.get();

            switch (msg.what) {
                case MSG_RECOGNIZER_START_LISTENING:

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        // turn off beep sound
                        //target.mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
                    }
                    if (!target.mIsListening){
                        target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
                        target.mIsListening = true;
                        Log.d("IncomingHandler", "message start listening");
                    }
                    break;

                case MSG_RECOGNIZER_CANCEL:
                    target.mSpeechRecognizer.cancel();
                    target.mIsListening = false;
                    Log.d("IncomingHandler", "message canceled recognizer");
                    break;
            }
        }
    }


....
}
Community
  • 1
  • 1
HorstB
  • 243
  • 1
  • 2
  • 5
  • The beep should be here... is the sound volume high enough for all streams ? http://developer.android.com/training/managing-audio/volume-playback.html – Florent Jul 21 '15 at 11:49

0 Answers0