androidspeech-recognitionvoice-recognitionrecognizer-intent

SpeechRecognizer not starting


INTRODUCTION

I need to implement the voice recognition in my code. I followed other posts here and some tutorials to get it, but it isn't working right for me.

APPROACH

This is code in onCreate to initialize it:

Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);

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());

The activity implements TextToSpeech.OnInitListener, so, I call the main method when this implementation is intialized in it's own method:

@Override
public void onInit(int status) {
    myMethod();
}

Then, inside myMethod() I start speech recognition like this:

mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

And finally, this is the listener for the results:

private final RecognitionListener mRecognitionListener = new RecognitionListener() {
    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("SPEECH", "onBufferReceived");
    }
    @Override
    public void onError(int error) {
        Log.d("SPEECH", "onError: " + error);

        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }
    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("SPEECH", "onEvent");
    }
    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("SPEECH", "onPartialResults");
    }
    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("SPEECH", "onReadyForSpeech");
    }
    @Override
    public void onResults(Bundle results) {
        Log.d("SPEECH", "onResult");

        matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    }
    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d("SPEECH", "onRmsChanged");
    }
    @Override
    public void onBeginningOfSpeech() {
        Log.d("SPEECH", "onBeginningOfSpeech");
    }
    @Override
    public void onEndOfSpeech() {
        Log.d("SPEECH", "onEndOfSpeech");
    }
};

When I do mSpeechRecognizer.startListening(mSpeechRecognizerIntent); It doesn't show no error or nothing wrong in the logcat, but the listener is not initialized, I don't see the LOGs in the LogCat, so I asume that it isn't being initilized well.

Maybe I'm not starting well the listener or what could be happening?

UPDATE-- activity structure

public class GameActivity extends Activity implements TextToSpeech.OnInitListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        tts = new TextToSpeech(this, this);

        Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(new SpeechListener());

        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());

        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }

    /*Method implemented by texttospeech*/
    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {
            /*set Language*/
            tts.setLanguage(Locale.getDefault());

            /*STARTS MAIN METHOD*/
            SpeechWhenMotion();
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    }

    /*Main method, does all the work*/
    public void SpeechWhenMotion() {
    }

Solution

  • According to the documentation speech recognizer must be invoked from the main thread. You are trying to start the recognizer in onInit callback from TTS engine. This is not a main thread, tts engine callback is executed in a separate thread.

    You need to run the ASR initializer in the main thread, you can init speech recognizer first in onCreate() method and then initialize text-to-speech.

    Alternatively, you can post handler to init ASR in TTS thread:

    handler.postDelayed(new Runnable() {
       @Override
       run() {
          MyASRInit()
       }
    }