How to get the result of voice recognition on EditBox? - android

I know there is like a ton topic and question regarding Voice Recognition and my question might be a stupid one too. but please bear with me guys.
I need to get the result of the speech recognition into an (Editable Text Box) instead of (Array List), the editable text box to allow the user to edit the result , just like a memo.
I found some questions like mine but I could not understand ,I am still a beginner comparing to you guys .
This is the code :
public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private ListView mlvTextMatches;
private Button mbtSpeak;
private Button reButton;
private EditText result;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
Toast.LENGTH_LONG).show();
result = (EditText) findViewById(R.id.out_text);
mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startActivity(new Intent(v.getContext(),KeyBoard.class));
}
});
checkVoiceRecognition();
}
public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {
ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (textMatchList.get(0).contains("search")) {
} else {
// populate the Matches
mlvTextMatches .setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,textMatchList));
}
}
//Result code for various error.
{
} if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
showToastMessage("Audio Error");
}else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
showToastMessage("Client Error");
}else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
showToastMessage("Network Error");
}else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
showToastMessage("No Match");
}else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
showToastMessage("Server Error");
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* Helper method to show the toast message
**/
void showToastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
This is the code after editing :
public class AVRScreen extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private Button mbtSpeak;
private Button reButton;
private EditText myEditText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr_screen);
Toast.makeText(this, "Press Speak! to Start Speeking",
Toast.LENGTH_LONG).show();
myEditText = (EditText) findViewById(R.id.out_text);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
reButton = (Button)findViewById(R.id.Replay1);
reButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startActivity(new Intent(v.getContext(),KeyBoard.class));
}
});
checkVoiceRecognition();
}
public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {
ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (textMatchList.get(0).contains("search")) {
} else {
// populate the Matches
myEditText.setText(textMatchList.toString());
// if the above does not look good
// for (String match : textMatchList) {
// myEditText.append(match + "\n"); // or whatever separator you want
// }
}
}
the second try is :
} else {
// populate the Matches
//myEditText.setText(textMatchList.toString());
// if the above does not look good
for (String match : textMatchList) {
myEditText.append(match + "\n"); // or whatever separator you want
}
}
}

if(resultCode == RESULT_OK) {
ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (textMatchList.get(0).contains("search")) {
} else {
// populate the Matches
result.setText(textMatchList.toString());
// if the above does not look good
// for (String match : textMatchList) {
// result.append(match + "\n"); // or whatever separator you want
// }
}

Related

Android studio, wait for one line of code to complete before starting next

Beginner question...
I have the following 4 lines of code.
The third line asks the user (with speech) whether he / she is certain about an action.
Then the fourth line calls a method called speechYesNo (included below) to listen for the users response.
The problem is that the speechYesNo method is called before the txtSpeech.speak line (3rd line) is finished speaking and it records the "Are you sure... " speech line as well as the users response.
For example, if the user responds "Yes", the result from the speechYesNo method will be something like... "Are you sure you would like to add xxxxx Yes".
I just want the result to be "yes".
Is there a way to force the third line (txtSpeech.speak) to finish before the speechYesNo() method is called?
Thanks
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
speechYesNo();
Here is the speechYesNo() method
public void speechYesNo() {
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());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 9);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
Here is the onActivityResult...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strResult = result.get(0);
Double dblStringComparison = 0.000;
String strFinalResult = "Unknown";
for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {
String strVal1 = strResult;
String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
Double dblTemp = myUtil.similarity(strVal1, strVal2 );
if(dblTemp > dblStringComparison){
dblStringComparison = dblTemp;
strFinalResult = arrAnimals.get(intCount);
}
}
atvAnimalName.setText(strFinalResult);
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
speechYesNo();
}
break;
case 9:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strYesNo0 = result2.get(0);
Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();
}
break;
}
}
Here is the full script
The openSpeechMode method will fire when user clicks an image and will record the users speech selection.
Once this is done I want the app to ask the user is he / she is sure about his / her selection.
If the user responds "yes" then I will do some more work.
The problem as per above is the the speech recorder is recording the "are you sure " question as well as the "yes" response.
public void openSpeechMode(View view) {
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());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
public void speechYesNo() {
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());
if(intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 9);
} else {
Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strResult = result.get(0);
Double dblStringComparison = 0.000;
String strFinalResult = "Unknown";
for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {
String strVal1 = strResult;
String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
Double dblTemp = myUtil.similarity(strVal1, strVal2 );
if(dblTemp > dblStringComparison){
dblStringComparison = dblTemp;
strFinalResult = arrAnimals.get(intCount);
}
}
atvAnimalName.setText(strFinalResult);
String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
// speechYesNo();
}
break;
case 9:
if(resultCode == RESULT_OK && data != null) {
ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String strYesNo0 = result2.get(0);
Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();
}
break;
}
}
Question
The problem is that the speechYesNo method is called before the
txtSpeech.speak
Solution
txtSpeech seems to be TextToSpeech class. thus, you should use UtteranceProgressListener for resolving your problem.
Try as below code.
txtSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
}
#Override
public void onDone(String utteranceId) {
speechYesNo();
}
#Override
public void onError(String utteranceId) {
}
});
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);

How to get past information while converting speech to text in android?

As i am developing an android app and wants to convert speech into text, i am using built-in Google speech input activity to convert voice into text. I need past information but it continuously get cleared i got only current response. How need to handle same as google voice keyboard. As i talk it included to current String instep of clear.
MainActivity .java
public class MainActivity extends AppCompatActivity
{
private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
promptSpeechInput();
}
});
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));
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);
try
{
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
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)
{
final ArrayList<String> result= data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
If you only want to save one previously detected String, for this to achieve, you need to make a global String variable and store the value in that variable from results list.(Save the same String as you are setting on text view). But if you want to save all the strings, you need to make global String Arraylist and add all those string in that array list. Below is the code for that.
private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
private List<String> previousStringList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previousStringList = new ArrayList<>();
txtSpeechInput = findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
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));
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
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) {
final ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
if (result.get(0) != null) {
previousStringList.add(result.get(0));
}
}
break;
}
}
}
Hope that helps you.If you don't understand anything feel free to ask. If you don't want to save same String twice(already saved string), just replace below conditional line of code..
if (result.get(0) != null && !previousStringList.contains(result.get(0))) {
previousStringList.add(result.get(0));
}
The words get stored in Arraylists.
You can see an example of the implementation here, it works fine. The app stores the words, and then also performs the action requested.
https://github.com/saumyabahu/Travel-Safe/blob/master/MainActivity.java
public class MainActivity extends AppCompatActivity {
private SpeechRecognizer speechRecognizer;
private Intent intentRecognizer;
private EditText txtSpeechInput;
private ImageButton btnSpeak;
//this is the string in which words get stored and past strings with left to right cursor.
String previous = " ";
// ArrayList result = null;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
// ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED );
txtSpeechInput = findViewById( R.id.ed );
btnSpeak = (ImageButton) findViewById( R.id.iButton );
btnSpeak.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
} );
}
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 ) );
intent.putExtra( RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000 );
try {
startActivityForResult( intent, REQ_CODE_SPEECH_INPUT );
} catch (ActivityNotFoundException a) {
Toast.makeText( getApplicationContext(),
getString( R.string.speech_not_supported ),
Toast.LENGTH_SHORT ).show();
}
}
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) {
final ArrayList<String> result = data
.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );
//this is the real problem.
txtSpeechInput.setText( previous + " " + result.get( 0 ) );
previous = txtSpeechInput.getText().toString();
txtSpeechInput.setText( previous );
}
break;
}
}
}
}

Android Offline Speech Recognition to Display Only Word

I need help with android speech-text. Is it possible to display only the first word that was detected?like when the user inputs "the cat is playing" then the text that will only appear on the text box is the word "the".
The code that I used:
protected static final int RESULT_SPEECH = 1;
private Button btn_mic;
private TextView txtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_level1);
txtText = (TextView) findViewById(R.id.txt);
btn_mic = (Button) findViewById(R.id.mic);
btn_mic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#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;
}
}
}
}
thanks in advance. :)
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(text.size()>0){
String firstOne=text.get(0);
if(firstOne.contains(" ")){
String firstWord=firstOne.substring(0, firstOne.indexOf(' '));
txtText.setText(text.get(0));
}else{
txtText.setText(text.get(0));
}
}

startActivityForResult doesnt return to onActivityResult in android

The first time I call startActivityForResult() with requestCode x it return to onActivityResult().But the second time I call startActivityForResult() within the same activity with diffrent requestCode it doesnt return to the onActivityResult() and just proceed
with the code.
I have tried to add this property to the manifest andter the activity
android:noHistory
Why it doesnt return to onActivityResult()?And how i can fix it?
Thanks a lot,
Leon
Edit: Here is my code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
else if(RESULT_SPEECH == requestCode)
{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textFromSpeech = text.get(0);
}
}
}
private void getTextFromSpeech()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tts_);
/*
* New Intent purely for the purposes of checking the user data
*/
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}

using voice command in android how to navigate pages

i want to develop a application to navigate one page to another page using voice command
here is my code
public class mainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ArrayList<String> StoredCommand = new ArrayList<String>();
private static final String TAG = "VoiceRecognition";
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private static final Context View = null;
private ListView mList;
private Handler mHandler;
private Spinner mSupportedLanguageView;
/**
* Called with the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
StoredCommand.add("Path Recoder");
StoredCommand.add("Path Selector");
StoredCommand.add("Stop");
StoredCommand.add("Pause");
// Inflate our UI from its XML layout description.
setContentView(R.layout.main);
// Get display items for later interaction
Button speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
mSupportedLanguageView = (Spinner) findViewById(R.id.supported_languages);
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
// Most of the applications do not have to handle the voice settings. If the application
// does not require a recognition in a specific language (i.e., different from the system
// locale), the application does not need to read the voice settings.
refreshVoiceSettings();
}
/**
* Handle the click on the start recognition button.
*/
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startVoiceRecognitionActivity();
}
}
/**
* Fire an intent to start the speech recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
// Display an hint to the user about what he should say.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
// Given an hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Specify how many results you want to receive. The results will be sorted
// where the first result is the one with higher confidence.
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
// Specify the recognition language. This parameter has to be specified only if the
// recognition has to be done in a specific language and not the default one (i.e., the
// system locale). Most of the applications do not have to set this parameter.
if (!mSupportedLanguageView.getSelectedItem().toString().equals("Default")) {
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
mSupportedLanguageView.getSelectedItem().toString());
}
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handle the results from the recognition activity.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
StringBuilder sb=new StringBuilder() ;
for (String match:matches){
switch(resultCode) {
case RESULT_OK:
Log.i(TAG, "RESULT_OK");
if(StoredCommand==matches)
{
//Button next=(Button)findViewById(R.id.btn_speak);
if (matches.contains("Path Recoder"))
{
Intent myIntent = new Intent(View, PahtRecoder.class);
startActivityForResult(myIntent, 0);
}
else if(matches.contains("path selector"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
else if(matches.contains("stop"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
else if(matches.contains("start"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
}
else
{
Log.i(TAG, "COMMAND_NOT_MATCHING");
}
break;
case RESULT_CANCELED:
Log.i(TAG, "RESULT_CANCELED");
break;
case RecognizerIntent.RESULT_AUDIO_ERROR:
Log.i(TAG, "RESULT_AUDIO_ERROR");
break;
case RecognizerIntent.RESULT_CLIENT_ERROR:
Log.i(TAG, "RESULT_CLIENT_ERROR");
break;
case RecognizerIntent.RESULT_NETWORK_ERROR:
Log.i(TAG, "RESULT_NETWORK_ERROR");
break;
case RecognizerIntent.RESULT_NO_MATCH:
Log.i(TAG, "RESULT_NO_MATCH");
break;
case RecognizerIntent.RESULT_SERVER_ERROR:
Log.i(TAG, "RESULT_SERVER_ERROR");
break;
default:
Log.i(TAG, "RESULT_UNKNOWN");
break;
}
}
}
else{
Log.e("TAG", "Recognition is Failed");
}
super.onActivityResult(requestCode, resultCode, data);
}
private void refreshVoiceSettings() {
Log.i(TAG, "Sending broadcast");
sendOrderedBroadcast(RecognizerIntent.getVoiceDetailsIntent(this), null,
new SupportedLanguageBroadcastReceiver(), null, Activity.RESULT_OK, null, null);
}
private void updateSupportedLanguages(List<String> languages) {
// We add "Default" at the beginning of the list to simulate default language.
languages.add(0, "Default");
SpinnerAdapter adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, languages.toArray(
new String[languages.size()]));
mSupportedLanguageView.setAdapter(adapter);
}
private void updateLanguagePreference(String language) {
TextView textView = (TextView) findViewById(R.id.language_preference);
textView.setText(language);
}
/**
* Handles the response of the broadcast request about the recognizer supported languages.
*
* The receiver is required only if the application wants to do recognition in a specific
* language.
*/
private class SupportedLanguageBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, final Intent intent) {
Log.i(TAG, "Receiving broadcast " + intent);
final Bundle extra = getResultExtras(false);
if (getResultCode() != Activity.RESULT_OK) {
mHandler.post(new Runnable() {
#Override
public void run() {
showToast("Error code:" + getResultCode());
}
});
}
if (extra == null) {
mHandler.post(new Runnable() {
#Override
public void run() {
showToast("No extra");
}
});
}
if (extra.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
mHandler.post(new Runnable() {
#Override
public void run() {
updateSupportedLanguages(extra.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES));
}
});
}
if (extra.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
mHandler.post(new Runnable() {
#Override
public void run() {
updateLanguagePreference(
extra.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE));
}
});
}
}
private void showToast(String text) {
Toast.makeText(mainActivity.this, text, 1000).show();
}
}
according to the stored command i need to navigate those pages.but result is Google voice option work but commands are not working.is there any wrong of my pattern matching...please give me solution for that.
thank you
Try this out as a minimal implementation of onActivityResult():
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
for (String bestMatch:matches)
{
if (bestMatch.equalsIgnoreCase("Path Recoder"))
{
Intent myIntent = new Intent(View, PahtRecoder.class);
startActivityForResult(myIntent, 0);
}
else if(bestMatch.equalsIgnoreCase("Path Selector"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
else if(bestMatch.equalsIgnoreCase("Stop"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
else if(bestMatch.equalsIgnoreCase("Pause"))
{
Intent myIntent = new Intent(View, Pahtselector.class);
startActivityForResult(myIntent, 0);
}
else
{
Log.i(TAG, "COMMAND_NOT_MATCHING");
}
}
}
Further Update: My initial post was wrong - StoredCommand should not be used; on older speech recognition platforms they would be provided with a list of possible utterances and the engine would try to match what you say against the possibilities. However, the default engine on Android does not need this. By the way, what does your mList display?
Note also that I have not tested any of the code above...
Hi all this is the code for navigate from one page to another page using voice command.It will useful any one
public class mainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "VoiceRecognition";
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private static final Context View = null;
private ListView mList;
private Handler mHandler;
private Spinner mSupportedLanguageView;
/**
* Called with the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
// Inflate our UI from its XML layout description.
setContentView(R.layout.main);
// Get display items for later interaction
Button speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
mSupportedLanguageView = (Spinner) findViewById(R.id.supported_languages);
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
// Most of the applications do not have to handle the voice settings. If the application
// does not require a recognition in a specific language (i.e., different from the system
// locale), the application does not need to read the voice settings.
refreshVoiceSettings();
}
/**
* Handle the click on the start recognition button.
*/
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startVoiceRecognitionActivity();
}
}
/**
* Fire an intent to start the speech recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
// Display an hint to the user about what he should say.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
// Given an hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Specify how many results you want to receive. The results will be sorted
// where the first result is the one with higher confidence.
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
// Specify the recognition language. This parameter has to be specified only if the
// recognition has to be done in a specific language and not the default one (i.e., the
// system locale). Most of the applications do not have to set this parameter.
if (!mSupportedLanguageView.getSelectedItem().toString().equals("Default")) {
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
mSupportedLanguageView.getSelectedItem().toString());
}
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handle the results from the recognition activity.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
for (String bestMatch : matches) {
if (bestMatch.contains("record") || bestMatch.contains("cod") || bestMatch.contains("ed")) {
// Intent myIntent = new Intent(View, PahtRecoder.class);
// startActivityForResult(myIntent, 0);
Intent my = new Intent(getApplicationContext(),
PathRecorderStart.class);
startActivityForResult(my, 0);
}
else if (bestMatch.contains("select") || bestMatch.contains("elect") || bestMatch.contains("ct")) {
// Intent myIntent = new Intent(View, PahtRecoder.class);
// startActivityForResult(myIntent, 0);
Intent my = new Intent(getApplicationContext(),
PathSelectorOptions.class);
startActivityForResult(my, 0);
}
else {
Log.i(TAG, "COMMAND_NOT_MATCHING");
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void refreshVoiceSettings() {
Log.i(TAG, "Sending broadcast");
sendOrderedBroadcast(RecognizerIntent.getVoiceDetailsIntent(this), null,
new SupportedLanguageBroadcastReceiver(), null, Activity.RESULT_OK, null, null);
}
private void updateSupportedLanguages(List<String> languages) {
// We add "Default" at the beginning of the list to simulate default language.
languages.add(0, "Default");
SpinnerAdapter adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, languages.toArray(
new String[languages.size()]));
mSupportedLanguageView.setAdapter(adapter);
}
private void updateLanguagePreference(String language) {
TextView textView = (TextView) findViewById(R.id.language_preference);
textView.setText(language);
}
/**
* Handles the response of the broadcast request about the recognizer supported languages.
*
* The receiver is required only if the application wants to do recognition in a specific
* language.
*/
private class SupportedLanguageBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, final Intent intent) {
Log.i(TAG, "Receiving broadcast " + intent);
final Bundle extra = getResultExtras(false);
if (getResultCode() != Activity.RESULT_OK) {
mHandler.post(new Runnable() {
#Override
public void run() {
showToast("Error code:" + getResultCode());
}
});
}
if (extra == null) {
mHandler.post(new Runnable() {
#Override
public void run() {
showToast("No extra");
}
});
}
if (extra.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
mHandler.post(new Runnable() {
#Override
public void run() {
updateSupportedLanguages(extra.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES));
}
});
}
if (extra.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
mHandler.post(new Runnable() {
#Override
public void run() {
updateLanguagePreference(
extra.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE));
}
});
}
}
private void showToast(String text) {
Toast.makeText(mainActivity.this, text, 1000).show();
}
}
}

Categories

Resources