如何从Google的“语音操作”中获取语音识别结果?

发布于 2024-12-10 12:10:43 字数 1018 浏览 0 评论 0原文

尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-12-17 12:10:43

如果结果不是 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 of resultCode 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 calls onActivityResult 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文