Android中语音识别的多个结果

时间:2011-02-21 11:21:53

标签: android

您好我正在尝试从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);

}

3 个答案:

答案 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)

嗯,我不完全确定你在这里要问的是什么,你是否收到错误,或者你只是要求澄清代码的作用?

关于语音识别的

I wrote a newbies guide,可能会对您有所帮助

此致

答案 2 :(得分:0)

我认为你做对了。在sample of the demos中,显示所有匹配项  尝试使用他们使用的VOICE_RECOGNITION_REQUEST_CODE值:1234

<强>编辑: 那么你对REQUEST_CODE是正确的,但你的代码实际上几乎与演示中的相同,对我来说非常合适。这可能是服务本身的问题吗?你用的是哪种语言?

问候。