0

我正在使用 Android 语音识别意图,但我想知道用户设置了什么语言来进行识别。RecognizerIntent上的文档暗示您可以从意图数据中获取此信息,但我一直为空。

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

我得到这样的结果:

/**
 * 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.getStringExtra(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);
}
4

1 回答 1

1

看来需要发广播询问语音识别中配置了什么语言。所以,序列是

  • 调用 ACTION_RECOGNIZE_SPEECH Intent。
  • 收到对此 Intent 的响应后,广播 ACTION_GET_LANGUAGE_DETAILS Intent。
  • 收到对此广播请求的响应后,您可以处理原始 Intent 返回的文本。

下面的代码:

/**
 * 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.
  }

}

于 2011-05-20T08:44:57.663 回答