I want to use VoiceRecognition in my application, but this application needs to install voice search.
I don't want the user to have to install another other application then return to my application to run it. I want voice search to be installed from my application, or alternatively I'd like to find a tutorial to on how to add Voice Search capability to my application.
What can I do?
Use the RecognizerIntent to fire the speech recognizer installed on your device
This can be done in a few simple steps:
Create some sort of button in your activity, and place the following code in its OnClickListener:
// Define MY_REQUEST_CODE as an int constant in your activity...I use ints in the 10000s
startVoiceRecognitionActivity(MY_REQUEST_CODE, "Say something.");
Override the onActivityResult() method in your activity. In the implementation, place a switch block or if statement to run some logic when the requestCode argument matches your MY_REQUEST_CODE constant. Logic similar to the following will get you the list of results the speech recognition activity thought it heard:
ArrayList keywordMatches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
You may get 0 or many matches from the recognizer. Be sure to handle all cases.
In some cases, the speech recognizer may not even be on the device. Try to handle that where you call startVoiceRecognitionActivity().
I found this tutorial :
http://www.jameselsey.co.uk/blogs/techblog/android-how-to-implement-voice-recognition-a-nice-easy-tutorial/
hope this helps.
Android Open Source VoiceRecognition Example
Here is a Simple way to Handle Voice Search
Step 1 Call this method on button click
public void startVoiceRecognition() {
Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
intent.putExtra("android.speech.extra.PROMPT", "Speak Now");
this.mContainerActivity.startActivityForResult(intent, 3012);
}
Step 2 Override onActivityResult method
# Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 3012 && resultCode == RESULT_OK) {
ArrayList < String > matches = data.getStringArrayListExtra("android.speech.extra.RESULTS");
String result= matches.get(0);
//Consume result
edittext.setText(result);
}
}
Thats all, DONE
Related
I created an Activity with a button, which when clicked, starts an Intent to launch the voice input as follows:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
startActivityForResult(intent, SPEECH_REQUEST_CODE);
Then, I get the results and display them in a dialog so that the user can select the closest match.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
final ArrayList<String> list_voice_input = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// I use this ArrayList to create a dialog.
}
super.onActivityResult(requestCode, resultCode, data);
}
So the Activity works as follows: Click the button, speak, and then a dialog pops up listing the closest matches and you can select one.
I have used a button-click to implement this. But Android's keyboard already has inbuilt voice input. When I use it to speak, it types out what I am saying, automatically detecting the closest match.
What I need:
I wish to make use of the default keyboard instead of a button, detecting when the user has chosen to speak instead of type (in a text field), and display a dialog box with the closest matches.
Any ideas on how I can do this?
Edit:
My question was marked as a duplicate, but I am not trying to get rid of the pop-up dialog that comes when you are recording your voice. My question is different.
When you are typing text, in the Android keyboard, there is a voice input option already inbuilt. You can use that to speak text. So, my question is, instead of automically printing out the closest match, can I show the user a list of closest matches in a dialog and ask them to select one?
No, you can't change the behavior of the default keyboard like that. There's no API to do so, to override what a single key does. If you want this you need to interact with the speech to text API yourself, without using the keyboard. This is built into Android (assuming the phone has a voice provider downloaded), and the API allows the voice provider to return multiple possible results. Whether a given voice provider does or not (remember the user and OEM can install any speech to text provider they want) is something you'd have to experiment and find out.
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 ...
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.
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.
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.
I am making certain android app that requires user to choose a place. I am planning to user google places API.Link: https://developers.google.com/places/android/
This API gives gives nice way to do so through through Place Picker. https://developers.google.com/places/android/placepicker
Now suppose I want to only show user places of food type (restaurant, hotel etc.) . Using Places Browser API this can be done by adding "type=restraunts" attribute in the requests.
Is there a way to show only places of certain types using the Google Places Picker for android ?
We are in 2017 and apparently this feature is still not implemented yet.
Even so, you can check the type(s) of the selected place on the activityResult and use it. In my case, I wanted the app only accept Hospitals and related services, so here's the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//...
boolean isAnHospital = false;
selectedplace = PlacePicker.getPlace(this, data);
for (int i : selectedplace.getPlaceTypes()) {
if (i == Place.TYPE_DOCTOR || i == Place.TYPE_HEALTH || i == Place.TYPE_HOSPITAL) {
isAnHospital = true;
break;
}
}
if(isAnHospital){
//Right type of place
}else{
//Tell to the user to select an appropriate place
}
Hope it helps in some way. Sorry for the bad english.
This is still not possible (14 September 2015).
Here is the open issue.
(info copied from related post, and reverified)
I have a working application that I would like to add voice commands. The current application transmits data back and forth over bluetooth on a periodic (timer) basis. The user can press Buttons and NumberPickers to modify the data being sent over bluetooth. There is also data received from the bluetooth link, and displayed in textViews. This application is currently working correctly.
What I would like to do is add voice command capability, so that the user has either the choice of pressing the Buttons/NumberPickers, or can change the values with only voice commands.
I have tested some of the Speech-to-Text examples that can be found on various websites. I have succesfully tested an App that uses RecognizerIntent. Upon a button press, a dialog pops up and you can speak words or phrases, and it correctly displays the result on the screen.
So, I think that I am close, but I'm not really sure how I can combine the Speech-to-Text with my current Bluetooth App. I don't want the user to have to press a button, I just want the App to be constantly listening. Also, I don't want the pop-up Voice Dialog on the screen.
My hardware is a Samsung tablet running Android 4.1.
I am relatively new to Android programming, so any advice (no matter how basic) is appreciated. Thanks.
To prevent the pop-up Voice Dialog on the screen, you can use the ACTION_RECOGNIZE_SPEECH intent:
private static int SR_CODE = 123;
/**
* Initializes the speech recognizer and starts listening to the user input
*/
private void listen() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//Specify language
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH)
// Specify language model
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Specify how many results to receive
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
// Start listening
startActivityForResult(intent, SR_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SR_CODE && resultCode == RESULT_OK) {
if(data!=null) {
//Retrieves the best list SR result
ArrayList<String> nBestList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String bestResult = nBestList.get(0);
Toast.makeText(getApplicationContext(), bestResult, Toast.LENGTH_LONG).show;
}else {
//Reports error in recognition error in log
Log.e(LOGTAG, "Recognition was not successful");
}
}
Concerning the other issue, " I don't want the user to have to press a button, I just want the App to be constantly listening":
I'll recommend using CMUSphinx to recognize speech continuously. To achieve continuous speech recognition using google speech recognition api, you might have to resort to a loop in a background service which will take too much resources and drains the device battery.
On the other hand, Pocketsphinx works really great. It's fast enough to spot a key phrase and recognize voice commands behind the lock screen without users touching their device. And it does all this offline.
You can try the demo.
If you really want to use google's api as I've demonstrated above, see this
I have android's Speech To Text API to speak something to the phone and convert it into text. By default, if one stops speaking to the microphone, the API assumes that the user is done talking and returns the text from the input speech.
For my application, the user might have long pauses between her consecutive sentences. How can I configure Android's speech to text API to consider the end of the speech only when I ask it to and not do that as soon as the speaker takes a small pause between sentences? Thanks!
Here is my current implementation which simply converts speech to text as soon as the user takes a small pause between sentences:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
The API has 3 EXTRAs for that
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
But note that the API also says that "depending on the recognizer implementation, these values may have no effect", so you just have to test with the implementation that you are using if they have any effect or not. (I haven't done this test myself, so it would be great if you added a comment to this answer reporting your test results.)
Prior to Android 4.1 (or users of the Google Search/Now app) this will work for you:
int someValue = 5;
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, Long.valueOf(someValue * 1000L));
Unfortunately later versions no longer react to this parameter, a great shame as it makes lengthy note taking or email composing impossible....
I have brought the issue to their attention.