开发者

Android Voice Recognition All results in one index of array

I'm trying to parse the results from an Android Voice Recognition Activity, and have found that all of the words (separated by a space) are in the first index of the array.

I was expecting it to put all words into each index of the array.

private void startVoiceRecognitionActivity() 
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    long wait = 10000;
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, wait);

    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

/**
 * Handle the results from the recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && 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);
        /*mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches)); */

        //start intent after we get list of items
        mListIntent = new Intent();
  开发者_C百科      mListIntent.setClassName(Consts.CLASS_PATH,
        ActivityUtil.getInstance().getClassName()); //get class name based
        //on current activity

        //now set array 'matches' from above and send to next activity...
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(Consts.BUNDLE_KEY_VOICE_LIST, matches);
        mListIntent.putExtras(bundle);

        startActivity(mListIntent);

        //TODO: also how do we add to google dictionary?

    }

    super.onActivityResult(requestCode, resultCode, data);
}


You have an extra quote on this line:

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now" ");

Just remove it.
It might work.


Just found this now. If anyone else find this, the problem isn't the extra speech mark (though that will stop the code compiling).

The problem is the user's understanding of what is returned in the matches (RecognizerIntent.EXTRA_RESULTS). This array does not have one entry for every word, it has one entry for every match the speech recognizer has for what it think you might have said.

E.g. if you say "I wrote on Stack Overflow", the array might be:

["I wrote on stack overflow",
 "I boat a stack over flow",
 "Ire oh ton stack over flow"]

If you want to get an array of the words, I'd suggest taking the first match string and splitting on all the spaces.


I have given a running code you can just copy paste it and you are done.. here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜