问题
Is there a way to issue a voice command something like:
OK GOOGLE ASK XXX Some App Specific Question or Command
And have it launch "APP" with the recognized text: "Some App Specific Question or Command"
My app has speech recognition as a service ... but when using my APP I can't ask questions that OK Google can handle ...
回答1:
Through the Voice Actions API, your app can register for system actions, one of which is 'search' (so you could do 'search for Some Question or command on APP').
In the past, some developers were able to submit a custom voice action request. Upon approval, users could do a specific action with your app via voice. This is no longer an option.
回答2:
This is actually pretty simple, With the built in voice Actions API you can do that both in online and offline mode. Here a short demo for you,
First prompt the user to input some voice,
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
This will bring up the built in Google speech input screen and will take the voice inputs. Now after a voice input check the result and get the voice into a converted string,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// here the string converted from your voice
String converted_text = (result.get(0);
}
break;
}
}
}
Now you can manipulate the string in any way you want or Compare them with pre-defined action strings to execute a specific action and many more....
UPDATE:
To make the app work on after saying a specific command e.g. "OK Google", Just define a static String called "OK Google" and compare each voice input with this pre-defined String. If that matches the "OK Google" String then move to the next worlds and execute the instructions. For example,
"OK Google speak the the current time"
Here you can compare the first two words "OK Google" if that matches your pre-defined String move to the next words which is "speak the current time". For this you may save a set of arrays containing your commands like "speak the current time" will speak out the time.
To make it look more intelligent you can implement a background service and keeps listening to user's voice input.
PS: I'm not sure if that would be an efficient way but it's just another approach of doing this.
回答3:
To integrate "OK Google" in your app is easy with following two steps.
First we need to declare in the manifest File
<activity..
<intent-filter>
<action android:name="com.google.android.gms.actions.SEARCH_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
/>
Now we need to declare in the Activity onCreate
if (getIntent().getAction() != null && getIntent().getAction().equals("com.google.android.gms.actions.SEARCH_ACTION")) {
query = getIntent().getStringExtra(SearchManager.QUERY);
Log.e("Query:",query); //query is the search word
}
User should follow the syntax to detect by "Ok google", When a user says, “OK Google, search for phrase on app name”, Google first checks if there is an app called app name installed which has declared itself to be capable of handling such queries.
来源:https://stackoverflow.com/questions/30689606/android-app-integrated-with-ok-google