Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpeechRecognizer offline ERROR_NO_MATCH

SpeechRecognizer return ERROR_NO_MATCH in onResults when the device is offline while it's returning the partial results in onPartialResults() call back. The last time I played around with SpeechRecognizer it was working fine offline, I wonder if anyone has found a solution to it.

like image 812
feisal Avatar asked Jun 04 '15 21:06

feisal


2 Answers

As a work around I use the partialResults returned in onPartialResults(). In the returned bundle "SpeechRecognizer.RESULTS_RECOGNITION" has all the terms minus the last term and "android.speech.extra.UNSTABLE_TEXT" has the last missing recognized term.

    @Override
public void onPartialResults(Bundle partialResults) {
    ArrayList<String> data = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    ArrayList<String> unstableData = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
    mResult = data.get(0) + unstableData.get(0);
}
like image 135
feisal Avatar answered Oct 31 '22 19:10

feisal


To make the answer a little bit more clear, you need to enable partial results first, and to call UNSTABLE_TEXT in a specific fashion:

// When creating the intent, set the partial flag to true
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true); 

// When requesting results in onPartialResults(), the UNSTABLE_TEXT parameter to getSTtringArrayList() must be in quotes
ArrayList<String> unstableMatches = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");

onPartialResults() gets called multiple times now and onError() still gets called with ERROR_NO_MATCH. I ended up using a solution similar to the one listed here: https://github.com/nenick/QuAcc/blob/master/app/src/main/java/de/nenick/quacc/speechrecognition/speech/RecognizerListenerWithOfflineWorkaround.java

In a nutshell:

  • Keep track of partial results and whether an error was shown
  • Reset both in onBeginningOfSpeech()
  • Store partial results in the variable when onPartialResults() gets called
  • When onError() gets called check if result is ERROR_NO_MATCH and combine SpeechRecognizer.RESULTS_RECOGNITION with "android.speech.extra.UNSTABLE_TEXT" into your partial results variable
  • Call onResults()
like image 26
autonomy Avatar answered Oct 31 '22 18:10

autonomy