Totic
Totic

Reputation: 1291

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

}

Upvotes: 0

Views: 2889

Answers (3)

Paul
Paul

Reputation: 21

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.

Upvotes: 2

mdelolmo
mdelolmo

Reputation: 6447

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.

Upvotes: 0

Jimmy
Jimmy

Reputation: 16428

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

Upvotes: 1

Related Questions