如何从Google的“语音操作”中获取语音识别结果?
尝试通过 RecognizerIntent.ACTION_WEB_SEARCH
在我的应用中使用 Google 的“语音操作”。根据文档,我应该能够通过 RecognizerIntent.EXTRA_RESULTS
获取语音识别结果。看来下面代码中的 onActivityResult() 是在 startActivityForResult()
之后立即调用的,而且结果还不好。有人可以帮忙吗?谢谢!
int VOICE_ACTIONS_CODE = 1234 ;
Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH) ;
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) ;
startActivityForResult(intent, VOICE_ACTIONS_CODE) ;
. . .
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == VOICE_ACTIONS_CODE) {
if(resultCode == RESULT_OK) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) ;
// The following should print speech recog. results
Log.w("Results from Voice Actions:", result.get(0)) ;
}
}
super.onActivityResult(requestCode, resultCode, data) ;
}
Trying to use Google's "Voice Actions" in my app via RecognizerIntent.ACTION_WEB_SEARCH
. According to documentation, I should be able to get the speech recognition results via RecognizerIntent.EXTRA_RESULTS
. It seems onActivityResult()
in the code below is called immediately after startActivityForResult()
and the results is not OK yet. Can anyone help? Thanks!
int VOICE_ACTIONS_CODE = 1234 ;
Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH) ;
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) ;
startActivityForResult(intent, VOICE_ACTIONS_CODE) ;
. . .
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == VOICE_ACTIONS_CODE) {
if(resultCode == RESULT_OK) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) ;
// The following should print speech recog. results
Log.w("Results from Voice Actions:", result.get(0)) ;
}
}
super.onActivityResult(requestCode, resultCode, data) ;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果结果不是
RESULT_OK
,是因为由于某种原因 Intent 进展不顺利。您的应用程序清单中是否有互联网连接和相应的权限?如果没有,请尝试添加它们。
顺便说一句,如果您没有得到
RESULT_OK
,那么您得到的resultCode
的值是多少?@@@@@@@@ 编辑@@@@@@@@
现在我完全理解你的问题。
问题如下,您使用操作
RecognizerIntent.ACTION_WEB_SEARCH
启动 Intent,并且根据 参考,它应该。它只是在显示“Speak Now”对话框时调用onActivityResult
,它不可能返回任何语音识别结果。尽管如此,它还是会触发相应的操作。但我同意你的观点,这种行为不是文档中描述的行为。
抱歉,我只能说这看起来像是 API 的实现错误。
If the result is not
RESULT_OK
is because for some reason the Intent didn't go well. Do you have an internet connection and the corrsponding permissions in your application manifest?If not, try adding them.
Btw, if you don't get
RESULT_OK
, what's the value ofresultCode
you get?@@@@@@@@ EDIT @@@@@@@@
Now I fully understand your question.
The issue is the following, you launch the Intent with action
RecognizerIntent.ACTION_WEB_SEARCH
and it never returns a result when, according to the reference, it should. It callsonActivityResult
just when the "Speak Now" dialog is displayed, it's impossible that it returns any result of the speech recognition.Nevertheless, it triggers the corresponding action. But I agree with you, the behaviour is not the one described in the docs.
I'm sorry, I just can say it looks like an API's implementation fault.