开发者

Multiple results for Voice Recognition in Android

Hi I am trying to get multiple values from the Android voice recognition, not just the best one.

I think I have to add one more flag, but I am not used to the android way of doing things. So far this is what I have:

(thanks)

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 == RE开发者_JAVA技巧SULT_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);

}


The flag you are looking for is EXTRA_MAX_RESULTS

If you wanted up to 10 results, your code would then look like this:

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);
}

Hope that helps.


Hmmm, I'm not entirely sure what you are trying to ask here, do you get an error or are you just asking for clarification on what the code does?

I wrote a newbies guide on voice recognition, that may help you

Regards


I think you're doing it right. In the sample of the demos, shows all the matches.
Try using the VOICE_RECOGNITION_REQUEST_CODE value they use: 1234.

Edited: Well you were right about REQUEST_CODE, but your code, which is actually almost the same given on the Demos, works perfectly for me. Could it be a Service itself issue? What language are you using?

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜