0

I have a simple app for voice recognition that prints all the possible string ArrayList decoded. The problem is that it only works if I don't stop/pause between words. If I have a slight pause (very short as if I was speking normally) the app stops. I looked at the parameter SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS but it didn't change anything.

Any clue from a voice recognition specialist?

Here is my code:

package com.bernard.vtt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends Activity implements RecognitionListener {
    private TextView mText;
    SpeechRecognizer speech = null;
    private Intent recognizerIntent;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button speakButton = (Button) findViewById(R.id.btn);
    mText = (TextView) findViewById(R.id.textView1);

    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
            "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

    speakButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            speech.startListening(recognizerIntent);
        }
    });

}

@Override
public void onResults(Bundle results) {
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    assert matches != null;
    for (String result : matches)
        text += result + "\n";

    mText.setText(text);

    speech.stopListening();
}

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int error) {

}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}
narb
  • 958
  • 1
  • 13
  • 39
  • Possible duplicate of [Continuous Speech Recognition Android](http://stackoverflow.com/questions/3148603/continuous-speech-recognition-android) – Nikolay Shmyrev Feb 01 '16 at 23:53

1 Answers1

4

I believe the built-in speech recognition does not continually run. It is designed to hear one voice input and give results. If you want to continually listen, you need to restart recognition on each onResults callback. I also believe SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS has a max value, which is why changing it has little effect.

nverbeek
  • 1,882
  • 17
  • 20
  • I have some doubts it is designed this way because I implemented the same test using the startactivity method and I get the same result. I will try though tomorrow, but I feel that doing so is a workaround. I would love seeing more response on my question. – narb Feb 01 '16 at 22:26
  • 2
    @narb Directly from [Google's documentaion](http://developer.android.com/reference/android/speech/SpeechRecognizer.html): "The implementation of this API is likely to stream audio to remote servers to perform speech recognition. As such this API is not intended to be used for continuous recognition, which would consume a significant amount of battery and bandwidth." – nverbeek Feb 01 '16 at 22:43
  • 1
    @narb To expand further, to perform continuous recognition on Android you may need to make use of a third-party offering such as Nuance. We tried to mangle SpeechRecognizer to perform continuous recognition as you look for here but in the end had to go with a third-party. You may also consider [CMUSphinx](http://cmusphinx.sourceforge.net/wiki/) for your needs. – nverbeek Feb 01 '16 at 23:36
  • Thanks! I'll have a look at it :) – narb Feb 02 '16 at 08:54