问题
I created a simple application inspired by this example in order to test all the available options (ie extra). I read about the EXTRA_PARTIAL_RESULTS extra and if I enable this option I should receive from the server any partial results related to a speech recognition. However, when I add this extra to the ACTION_RECOGNIZE_SPEECH
intent, the voice recognition does not work anymore: the list does not display any results.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE) {
switch(resultCode) {
case RESULT_OK:
Log.i(TAG, "RESULT_OK");
processResults(data);
break;
case RESULT_CANCELED:
Log.i(TAG, "RESULT_CANCELED");
break;
case RecognizerIntent.RESULT_AUDIO_ERROR:
Log.i(TAG, "RESULT_AUDIO_ERROR");
break;
case RecognizerIntent.RESULT_CLIENT_ERROR:
Log.i(TAG, "RESULT_CLIENT_ERROR");
break;
case RecognizerIntent.RESULT_NETWORK_ERROR:
Log.i(TAG, "RESULT_NETWORK_ERROR");
break;
case RecognizerIntent.RESULT_NO_MATCH:
Log.i(TAG, "RESULT_NO_MATCH");
break;
case RecognizerIntent.RESULT_SERVER_ERROR:
Log.i(TAG, "RESULT_SERVER_ERROR");
break;
default:
Log.i(TAG, "RESULT_UNKNOWN");
break;
}
}
Log.i(TAG, "Intent data: " + data);
super.onActivityResult(requestCode, resultCode, data);
}
private void processResults(Intent data) {
Log.i(TAG, "processResults()");
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// list of results
ListView listOfResults = (ListView)(findViewById(R.id.list_of_results));
listOfResults.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
// number of elements of above list
TextView resultsCount = (TextView)(findViewById(R.id.results_count));
resultsCount.setText(getString(R.string.results_count_label) + ": " + matches.size());
}
When this option is enabled, the number of elements in the list of results is equal to 1 and this one result is an empty string. What is the reason for this behavior?
ADDED DETAILS
I used the following code in order to enable EXTRA_PARTIAL_RESULTS
option (on Android 2.3.5).
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, ...);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, ...);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); // where VOICE_RECOGNITION_REQUEST_CODE is a "global variable"
However, enabling this option, the ArrayList<String> matches
in processResults
method has only one empty element.
回答1:
I suggest not relying on PARTIAL_RESULTS because the Google documentation says this: The server may ignore a request for partial results in some or all cases.
In my experience, Google almost always ignores requests for partial results.
However, I don't believe that setting EXTRA_PARTIAL_RESULTS
should stop normal EXTRA_RESULTS
from being returned. It should just have no effect. I have tested this in my code and adding EXTRA_PARTIAL_RESULTS
doesn't change the output.
Also, unless you are using SpeechRecognizer
, Android won't send you those error codes.
To see that EXTRA_PARTIAL_RESULTS
does not interfere with the results. Get the code from here then add the extra within this method:
private void sendRecognizeIntent()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say the magic word");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
来源:https://stackoverflow.com/questions/9859184/partial-results-using-speech-recognition