I'm building an Android application and I would like to create a voice command like Ok Google, but I'd like it to be "Ok House". Is there a way to customize that Google command and to create my own just changing a parameter?
Asked
Active
Viewed 324 times
1 Answers
0
You can use any speech command "Ok Google" is special for Google Home Assistant Have a look on this tutorial
https://itnext.io/using-voice-commands-in-an-android-app-b0be26d50958
and see how speech recognizer works on this gist
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);
}
};
you can make intent based on recognized speech

Ahmed Abdelazim
- 717
- 7
- 14
-
Thanks I'll give it a try. This is my first app and I think I'll have a lot of trouble on making this – LUCAS M SANTOS Sep 14 '18 at 13:10
-
Ahmed should I put this into main class or create a new one? If I need to create a new, what should I import? – LUCAS M SANTOS Sep 14 '18 at 13:26
-
Separate class and then import it into activity to call – Ahmed Abdelazim Sep 14 '18 at 17:18
-
Full source of tutorial project is on GitHub – Ahmed Abdelazim Sep 14 '18 at 17:19
-
Ahmed. I'm trying to do a listener like Google Assistant. Most codes that I found, I had to click on a button to start listening the user. But what I'm trying to do is to create a passive listener that will start the app when it hears a specific word (Just like google assistant). Is that possible? – LUCAS M SANTOS Sep 18 '18 at 12:27
-
@LUCASMSANTOS theoretically you can run a service in background. see this link: https://stackoverflow.com/questions/34573109/how-to-make-an-android-app-to-always-run-in-background But this is not practically can be done because this service will take the mic and prevent other apps from using it. see the issue: https://issuetracker.google.com/issues/36982217 – Ahmed Abdelazim Sep 19 '18 at 06:49
-
Thanks for your reply. Yesterday I was testing the command Ok Google and if I say "Okay Google open the app X" it works. My manager wants to do the same with the phrase "Ok consul" and when the app hears "Ok consul" it should open the app X. I'm trying to create a service in background that would be able to listen the user 24 hours and when it recognizes the main phrase "Ok Consul" it opens my app. – LUCAS M SANTOS Sep 19 '18 at 11:29
-
I'm still having a lot of difficults to find a way to implement this function :/ – LUCAS M SANTOS Sep 19 '18 at 11:29