开发者

Android speech recognition - get language used

I'm using the Android Speech Recognition intent but I would like to know what language the user has set to do the recognition. The docs on the RecognizerIntent imply that you can get this from the intent data, but I keep getting null.

Are these values on useable when calling the Intent? Is there another way to get this data?

Here's how I call the intent:

private void startVoiceRecognitionActivity() {
    Logger.i(AppConfig.LOGTAG, "startVoiceRecognitionActivity");
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

And I get the results like this:

/**
 * 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) {

     Logger.i(AppConfig.LOGTAG, "EXTRA_LANGUAGE = "+data.getStringExtr开发者_StackOverflow中文版a(RecognizerIntent.EXTRA_LANGUAGE));
     Logger.i(AppConfig.LOGTAG, "EXTRA_LANGUAGE_MODEL = "+data.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL));
     Logger.i(AppConfig.LOGTAG, "EXTRA_LANGUAGE_PREFERENCE = "+data.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE));

  } else {
     Toast.makeText(getApplicationContext(), "Voice recognition failed.", Toast.LENGTH_LONG).show();
  }
  super.onActivityResult(requestCode, resultCode, data);
}


It seems that you need to send a broadcast to ask what language is configured in the voice recognition. So, the sequence is

  • Call the ACTION_RECOGNIZE_SPEECH Intent.
  • On receipt of the response to this Intent, broadcast the ACTION_GET_LANGUAGE_DETAILS Intent.
  • On receipt of the respone to this broadcast request you can then process the text that the original Intent returned.

Code below:

/**
 * Handle the results from the recognition activity. First thing to do is 
 * to get the language...
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {

     Intent intent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
     LangBroadcastReceiver myBroadcastReceiver = new LangBroadcastReceiver(this, data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
     sendOrderedBroadcast(intent, null, myBroadcastReceiver, null, Activity.RESULT_OK, null, null);

  } else {
     Toast.makeText(getApplicationContext(), "Voice recognition failed.", Toast.LENGTH_LONG).show();
  }
  super.onActivityResult(requestCode, resultCode, data);
}



/**
 * After a voice recognition is performed, need to sent a broadcast to
 * request the language used. This BroadcastReceiver gets the response and
 * then processes the original recognisedText from the
 * ACTION_RECOGNIZE_SPEECH Intent.
 * 
 */
public class LangBroadcastReceiver extends BroadcastReceiver {
  ArrayList<String> recognisedText;
  Activity parentActivity;

  /**
   * Store these for later use...
   * @param activity
   * @param arrayList
   */
  LangBroadcastReceiver(Activity activity, ArrayList<String> arrayList) {
     recognisedText = arrayList;
     parentActivity = activity;
  }

  @Override
  public void onReceive(Context context, Intent intent) {
     Bundle results = getResultExtras(true);
     String lang = results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
     Log.d(AppConfig.LOGTAG, "MyBroadcastReceiver: Got 'EXTRA_LANGUAGE_PREFERENCE' = " + lang);
     // now handle the recognisedText with the known language.
  }

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜