Android 语音识别 - 获取使用的语言
我正在使用 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);
}
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.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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来需要发广播询问语音识别中配置的是什么语言。因此,序列是
代码如下:
}
It seems that you need to send a broadcast to ask what language is configured in the voice recognition. So, the sequence is
Code below:
}