我创建了一个受此示例启发的简单应用程序,以测试所有可用选项(即额外选项)。我阅读了EXTRA_PARTIAL_RESULTS
额外的内容,如果启用此选项,我应该从服务器收到与语音识别相关的任何部分结果。但是,当我将这个额外添加到ACTION_RECOGNIZE_SPEECH
意图时,语音识别不再起作用:列表不显示任何结果。
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());
}
启用此选项后,结果列表中的元素数等于 1,并且该结果为空字符串。这种行为的原因是什么?
添加的详细信息
我使用以下代码来启用EXTRA_PARTIAL_RESULTS
选项(在 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"
但是,启用此选项后,ArrayList<String> matches
inprocessResults
方法只有一个空元素。