android - 使用 "Ok Google"之类的短语通过语音开始语音识别?

标签 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 Now 一样,您只需说“Ok Google”,它就会打开语音识别器 Activity 并听取命令?

谢谢。

最佳答案

您需要编写一个连续语音识别服务。并根据您在语音时获得的输入检测您的触发短语并采取行动。

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

this question 的公认答案提供了一种实现类似目标的方法。

关于android - 使用 "Ok Google"之类的短语通过语音开始语音识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465687/

相关文章:

python - Freeze.py上的Tensorflow简单音频识别错误

Android语音识别和录音同时进行

Android 编程按钮字体大小(以像素为单位)

android - 如何以设定的时间间隔循环播放不同次数的声音-Android

android - 将图库滚动到下一页

python - 如何返回 ffmpeg 进程输出文件并将其传递给另一个函数?使用Python

android - 有没有办法在 Android 和 iOS 中识别 BLE 设备

android - 在 Google map SupportMapFragment 上绘制 2 个地理点之间的行车路线

swift - 在 SpeechKit 框架中连续收听用户语音并检测语音结束静音

android - 如何获取设备的默认 SpeechService 提供程序 - Android API 13