通过语音开始语音识别,例如"Ok Google"吗? [英] Start speech recognition thru voice with phrase like "Ok Google"?

查看:123
本文介绍了通过语音开始语音识别,例如"Ok Google"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用语音命令执行某些功能的应用程序.我从此处

I'm building an app that uses Voice Commands to perform certain functions. I got some codes working from here

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}

但是,这种方法需要通过单击按钮来激活.有没有办法通过语音命令来启动语音识别器?像Google即时一样,您只需说"Ok Google",然后它将打开语音识别器活动并收听命令?

However, this approach needs to be activated through a button click. Is there a way to start the speech recognizer thru a voice command as well? Like Google Now where you can just say "Ok Google", then it will open up the Speech Recognizer activity and listen for commands?

谢谢.

推荐答案

您将需要编写服务以进行连续语音识别.并根据语音输入获得的信息检测触发短语并采取行动.

You will need to write a service for continuous speech recognition. And based on the inputs you get as speech detect your trigger phrase and take action.

这可能会占用大量内存,您需要通过在适当的时间和屏幕上启动和停止服务来进行优化.

This may be memory intensive and you will need to optimize by starting and stopping services on appropriate times and screens.

对此问题的可接受答案此问题为实现类似目的提供了一种方法.

The accepted answer to this question provides a means to achieve a similar thing.

这篇关于通过语音开始语音识别,例如"Ok Google"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆