5

I want to convert speech to text in my app.. For this i am using recognitionListener interface Everything works fine but how to get the text updated and shown even while speaking( like in google now voice search)

I have set the RecognizerIntent.EXTRA_PARTIAL_RESULTS, to true And also used the onPartialResults(Bundle arg() method of the recognitionListener interface to set text By the whole text is getting displayed at once after speech recognition is complete But i want the realtime text to be displayed as the user speaks

my activity

public class MainActivity extends Activity implements RecognitionListener
{
private TextView returnedText;
private ToggleButton toggleButton;
private ProgressBar progressBar;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    returnedText = (TextView) findViewById(R.id.textView1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
    Button recordbtn = (Button) findViewById(R.id.mainButton);


    progressBar.setVisibility(View.INVISIBLE);
    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_PARTIAL_RESULTS, true);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);

    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setIndeterminate(true);
                    speech.startListening(recognizerIntent);
                } else {
                    progressBar.setIndeterminate(false);
                    progressBar.setVisibility(View.INVISIBLE);
                    speech.stopListening();
                }
            }
        });


    recordbtn.setOnLongClickListener(new OnLongClickListener(){

            @Override
            public boolean onLongClick(View p1)
            {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
                return true;
            }


        });



}

@Override
public void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    if (speech != null) {
        speech.destroy();
        Log.i(LOG_TAG, "destroy");
    }

}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
    progressBar.setIndeterminate(false);
    progressBar.setMax(10);
}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
    Log.i(LOG_TAG, "onEndOfSpeech");
    progressBar.setIndeterminate(true);
    toggleButton.setChecked(false);
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d(LOG_TAG, "FAILED " + errorMessage);
    returnedText.setText(errorMessage);
    toggleButton.setChecked(false);
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.i(LOG_TAG, "onEvent");
}

@Override
public void onPartialResults(Bundle arg0) {
    Log.i(LOG_TAG, "onPartialResults");

    ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";

    returnedText.setText(text);
}

@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.i(LOG_TAG, "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
    Log.i(LOG_TAG, "onResults");

}

@Override
public void onRmsChanged(float rmsdB) {
    Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
    progressBar.setProgress((int) rmsdB);

}

public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}

}

HOW TO MAKE THIS HAPPEN

user5894647
  • 544
  • 6
  • 15

1 Answers1

5

You can't achieve a realtime recognition with google API. In the best case you can achieve the same result than google when you are using OK Google or for example Recognition in Whatsup for write Text word by word adding to your intent:

recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);

The Speechrecognizer take his time to process all the info and split it as info to be able to use it in your app. you can check the next post to see if it help you optimizing your app: Make SpeechRecognizer Faster

Hope that it will help you !

Community
  • 1
  • 1
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95
  • Thanks for the suggestion bro ...i ll try it,, – user5894647 May 30 '16 at 09:37
  • 1
    hope it will help you! I have deal a lot with this shit, if I can help you with something else just let me know ;) – Francisco Durdin Garcia May 30 '16 at 09:38
  • bro, yeah forgot to ask,, do you know any cool idea of animating the rmschanged method so that user can know that the device is listening to him,,,for test cases i have used a progressbar,, any suggestion? !thanks – user5894647 May 30 '16 at 09:44
  • 1
    In my case iI had a "starting" button. And when the app is listening, it beat with an animation. I asked it in Stackoverflow, so you can see the idea here: http://stackoverflow.com/questions/37263308/pulsating-button-animation-android You can add that kind of idea to any view doing the same, just call onDraw with an animation boolean value and do whatever you want – Francisco Durdin Garcia May 30 '16 at 09:46