如何自定义"Ok Google"语音命令?



我正在构建一个Android应用程序,我想创建一个像Ok Google这样的语音命令,但我希望它是"Ok House"。 有没有办法自定义该Google命令并创建我自己的只是更改参数?

您可以使用任何语音命令"Ok Google"是Google Home Assistant特别的 看看这个教程

https://itnext.io/using-voice-commands-in-an-android-app-b0be26d50958

并了解语音识别器如何在此要点上工作

https://gist.github.com/efeyc/e03708f7d2f7d7b443e08c7fbce52968#file-pingpong_speechrecognizer-java

...
private void initSpeechRecognizer() {
// Create the speech recognizer and set the listener
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
speechRecognizer.setRecognitionListener(recognitionListener); 
// Create the intent with ACTION_RECOGNIZE_SPEECH 
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US);
listen();
}

public void listen() {
// startListening should be called on Main thread
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> speechRecognizer.startListening(speechIntent);
mainHandler.post(myRunnable);
}
...
RecognitionListener recognitionListener = new RecognitionListener() {
...
@Override
public void onError(int errorCode) { 
// ERROR_NO_MATCH states that we didn't get any valid input from the user
// ERROR_SPEECH_TIMEOUT is invoked after getting many ERROR_NO_MATCH 
// In these cases, let's restart listening.
// It is not recommended by Google to listen continuously user input, obviously it drains the battery as well,
// but for now let's ignore this warning
if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH) || (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)) {
listen();
}
}
@Override
public void onResults(Bundle bundle) {
// it returns 5 results as default.
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
// a method that finds the best matched player, and updates the view. 
findBestMatchPlayer(results);
}

};

您可以根据识别的语音做出意图

最新更新