您好我正在尝试从Android语音识别中获取多个值,而不仅仅是最佳值。
我想我必须再添加一个标志,但我不习惯以android的方式做事。 到目前为止,这就是我所拥有的:
(感谢)
private OnClickListener mSpeakListener = new OnClickListener(){
public void onClick(View v){
if (v.getId() == R.id.speech) {
bar.setVisibility(View.VISIBLE);
startVoiceRecognitionActivity();
}
}
};
/**
* Fire an intent to start the speech recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
startActivityForResult(intent,0);
}
/**
* Handle the results from the recognition activity.
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuilder sb = new StringBuilder();
for(String match: matches){
sb.append(match + ", ");
}
questionTextBox.setText(sb.toString());
Log.d(TAG,"Focus on Button");
okButton.requestFocus();
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 0 :(得分:2)
您要寻找的旗帜是EXTRA_MAX_RESULTS
如果您想获得最多10个结果,那么您的代码将如下所示:
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,10); //newly added flag
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
startActivityForResult(intent,0);
}
希望有所帮助。
答案 1 :(得分:1)
答案 2 :(得分:0)
我认为你做对了。在sample of the demos中,显示所有匹配项
尝试使用他们使用的VOICE_RECOGNITION_REQUEST_CODE
值:1234
。
<强>编辑:强>
那么你对REQUEST_CODE
是正确的,但你的代码实际上几乎与演示中的相同,对我来说非常合适。这可能是服务本身的问题吗?你用的是哪种语言?
问候。