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

问题描述 投票:0回答:1

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

android google-api voice-recognition
1个回答
0
投票

您可以使用任何语音命令“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);
  }


};

你可以根据公认的言论制定意图

© www.soinside.com 2019 - 2024. All rights reserved.