Start speech recognition thru voice with phrase like “Ok Google”?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 01:52:08

问题


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);
}

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?

Thanks.


回答1:


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.




回答2:


Continuous Speech recognition with help of Service :-

Android Speech Recognition as a service on Android 4.1 & 4.2

GitHub Sample :-

https://github.com/galrom/ContinuesVoiceRecognition

Responding to magic words like Ok Google

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

I have implemented same feature for Banking Project. I was triggering speech to text recognition on device shake



来源:https://stackoverflow.com/questions/36465687/start-speech-recognition-thru-voice-with-phrase-like-ok-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!