通过“Ok Google”这样的短语开始语音识别?

时间:2016-04-07 02:30:56

标签: android speech-recognition

我正在构建一个使用语音命令执行某些功能的应用。我从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”一样,那么它会打开Speech Recognizer活动并听取命令吗?

感谢。

2 个答案:

答案 0 :(得分:5)

您需要编写连续语音识别服务。并根据您在语音中获得的输入检测您的触发短语并采取措施。

这可能是内存密集型的,您需要通过在适当的时间和屏幕上启动和停止服务来进行优化。

this question的接受答案提供了实现类似事情的方法。

答案 1 :(得分:2)

在服务的帮助下持续进行语音识别: -

Android Speech Recognition as a service on Android 4.1 & 4.2

GitHub示例: -

https://github.com/galrom/ContinuesVoiceRecognition

回应Ok Google等神奇的词汇

https://github.com/cmusphinx/pocketsphinx-android-demo

我已经为银行项目实施了相同的功能。我在 device shake

上触发了文字识别语音