1

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() {
    }
masmic
  • 3,526
  • 11
  • 52
  • 105
  • According to the documentation http://developer.android.com/reference/android/speech/SpeechRecognizer.html speech recognizer must be invoked from the main thread. Is it the case for you? You probably call myMethod in some other thread, you should try to move it to UI one. It is also mentioned in the original discussion at http://stackoverflow.com/questions/14940657/android-speech-recognition-as-a-service-on-android-4-1-4-2/14950616#14950616 – Nikolay Shmyrev May 13 '14 at 14:20
  • @Nikolay Shmyrev yes it is in the main thread... I've seen all the existing post about this in stackoverflow and it should work... maybe it has something to do with my device or android version... I don't know... – masmic May 13 '14 at 14:24
  • texttospeech listener is not the main thread. – Nikolay Shmyrev May 13 '14 at 14:25
  • @Nikolay Shmyrev I don't understand what you mean very well, but... What I write is that my activity implement's TTS and I start the main method when onInit is called, but all this happens in the main activity – masmic May 13 '14 at 14:27
  • 1
    Try to init speech recognition without tts in onCreate first. – Nikolay Shmyrev May 13 '14 at 14:29
  • @Nikolay Shmyrev, I've disabled the TTS initialization and now I see the Log's of the listener working... But, then, how can I use booth? – masmic May 13 '14 at 14:43

1 Answers1

2

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()
   }
}
Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • I have this now in onCreate() `mSpeechRecognizer.startListening(mSpeechRecognizerIntent);` but I've disabled the TTS initialization that I also had in onCreate() `tts = new TextToSpeech(this, this`. I woul appreciate a lot a deeper explanation on how to do what you say. Is firs time I'm working with TTS an Speech and I'm a bit lost on what you say about ASR, I'll update the OP to let you see the structure of my activity and that could be more helpful for you too – masmic May 13 '14 at 14:56
  • Enable TTS back and it should work. The thing is that you startListening in onCreate, it doesn't matter where you initialize TTS. – Nikolay Shmyrev May 13 '14 at 15:46
  • But, I have to say that if I have disabled the TTS initialization, I see the logs of the speechlistener, but if I enable back the TTS, I don't see the logs. I've readed that the use the same API, maybe I have to disable/enable something before using one or other, cause they can't be used booth at same time? – masmic May 14 '14 at 06:50