Speech recognizer on two buttons to two edittexts - android

I have two edittext boxes and a button for each.
When I click the first button I want to envoke the speech recognizer and the result goes into the first editext box and the 2nd button fills the 2nd edittext box.
I'm using the following code
public void speech1(View view) {
final int REQUEST_CODE = 1;
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(i, REQUEST_CODE);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
public void speech2(View view) {
final int REQUEST_CODE = 2;
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(i, REQUEST_CODE);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(REQUEST_CODE==1){
EditText editText = (EditText)findViewById(R.id.speechText1);
editText.setText(thingsYouSaid.get(0), TextView.BufferType.EDITABLE);
}
if(REQUEST_CODE==2){
EditText editText = (EditText)findViewById(R.id.speechText2);
editText.setText(thingsYouSaid.get(0), TextView.BufferType.EDITABLE);
}
}
It wont fill in the correct edittext box just leaves them blank have been looking for a tutorial but cant find any examples of two speech inputs

Not everything in your life is described by tutorial, sometimes you want to think yourself. In your particular case your issue is that you are comparing a global variable RESULT_CODE instead of a local argument of onActivityResult,
if(REQUEST_CODE==1){
EditText editText = (EditText)findViewById(R.id.speechText1);
must instead be
if(requestCode==1){
EditText editText = (EditText)findViewById(R.id.speechText1);
The request code is passed to you through the argument, you need to use it.

Related

Voice to String using RecognizerIntent not working

I'm trying to include a microphone button where user can click it and it can prompt a voice input from the user. Then the voice input will be converted into String and shown in a TextView in my android app. For now, the voice input can be prompted but the converted String will not be shown in my TextView and there are no crashes or logcat. Just that it wont be shown in my TextView. All this is happening in a class which extends Fragment.
Below is the code for my microphone button :-
//Button to activate voice recognition
microphonebutton = (ImageButton) view.findViewById(R.id.button_microphone);
microphonebutton.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, Locale.getDefault());
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(getActivity().getApplicationContext(), "Your device does not support speech input !",Toast.LENGTH_SHORT).show();
}
}
});
The method onActivityResult:-
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 10:
if (resultCode == Activity.RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
texttotranslate.setText("");
texttotranslate.setText(result.get(0), TextView.BufferType.EDITABLE);
}
break;
}
}
I got these codes from a tutorial on youtube and it seemed to be working fine for the person who was presenting the tutorial.
You have a typo in the method onActivityResult. In the switch you are chechking the resultCode twice. First you should check the requestCode. The requestCode is the code which you defined in startActivityForResult, in your case 10.

Giving data to other activity not working actually

What is the code about:
With my code I can open a mic, speak into it and see the output.
Present Situation
Right now if I speak into the mic, I see the output and jump directly in other activity.
Requirement
I want take the output in the activity but before I text to much I show you the code.
Mainactivity.java
private TextView voiceInput;
private ImageView speakButton;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceInput = (TextView) findViewById(R.id.voiceInput);
speakButton = (ImageView) findViewById(R.id.btnSpeak);
speakButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
askSpeechInput();
}
});
}
// Showing google speech input dialog
private void askSpeechInput() {
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, "Sprechen Sie was ein");
//tent.putStringArrayListExtra("result",resultat);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
#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) {
Intent intent=new Intent(this,Zweites.class);
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
} }} <br>
In that I got the following problem:
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Ausgabe in andere Activity
voiceInput.setText(result.get(0));
Intent intent=new Intent(this,Zweites.class);
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
There it outputs the spoken input:
voiceInput.setText(result.get(0));
And afterwards it jumps into the activity.
How can I get this output in the activity without first display the output and jump in another activity?
I tried some things but donĀ“t come to a solution how to fix this problem.
Do you guys got some ideas?
Kind regards
If you simply want to pass the same text to your intent that you're displaying in your TextView, exchange
intent.putStringArrayListExtra("resultat",result);
with
intent.putExtra("resultat", result.get(0));
Then, in your Zweites activity, you can get the string like so:
Intent intent = getIntent();
String text = intent.getStringExtra("resultat");

How to start the same activity using startActivityForResult twice

So I am trying to implement calling the same activity twice, I understand there will be better ways to do this but right now I just want 2 separate data recordings. When I try to run this code, the Diastolic BP gets read in first which is unintentional. Can someone explain why this is happening please. Thank you.
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//The following is required when ^^^ this is used
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
startActivityForResult(i, SYSTOLIC_CHECK);
//A different request code is required per activity called
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
// TODO: Show the thumbnail to the user while the full picture is being
// processed.
}
else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Systolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
}
else if ((requestCode == DIASTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Diastolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
}
super.onActivityResult(requestCode, resultCode, data);
}
The API simply doesn't work that way - you can't queue up the launch multiple activities as you are trying to do.
The normal way to do this is to launch the first Activity with startActivityForResult, and when the first activity returns, launch the second one.
If I'm not mistaken, since you call your Diastolic activity as last, this is laid over you systolic activity.
So you Diastolic Activity will be the one the user interacts with first.
I just want to point out that what #GreyBeardedGeek said is true.
It probably would be better if you just start you systolic activity, then, when you receive the result, start you Diastolic Activity (in OnActivityResult).
This will prevent issues with your activities not starting in the order you wanted.
EDIT
Your code would look something like this then
...else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
System.out.println("Systolic BP: " + spokenText);
//OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
//call second activity
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
} ...
EDIT
Your activity could look like this.
public class VoiceActivity extends ActionBarActivity {
private String systole;
private String diastole;
private Button btnGetVoiceInput;
private final int SYSTOLIC_CHECK=1, DIASTOLIC_CHECK=2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnGetVoiceInput = (Button) findViewById(R.id.btnVoiceInput);
btnGetVoiceInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//The following is required when ^^^ this is used
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
startActivityForResult(i, SYSTOLIC_CHECK);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case SYSTOLIC_CHECK: {
if(resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
systole = results.get(0);
System.out.println("Systolic BP: " + systole);
Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
startActivityForResult(j, DIASTOLIC_CHECK);
}
break;
}
case DIASTOLIC_CHECK: {
if(resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
diastole = results.get(0);
Log.v("VoiceInput", "Diastolic BP: " + diastole);
doSomething();
}
break;
}
}
}
private void doSomething() {
//do what you want with both readings here
String bloodpressure = systole + " / " + diastole;
Log.v("Bloodpressure", bloodpressure);
}
}
After getting the 2 readings, you can do something with them.
This does still need some error handling, but I haven't used the voice input yet, so I don't know what values it returns/can return.

Append Text in edittext using speech to text

I have an edittext for which I am using speech to text. I would like to append text in the edittext instead of overwriting. How can I achieve that. Right now it captures a sentence and displays in edittext. Thanks
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);
et_text.setText("");
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),"Opps! Your device doesn't support Speech to Text", Toast.LENGTH_SHORT).show();
}
}
#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);
et_text.setText(text.get(0));
}
break;
}
}
}
You can simply use append method instead of setText:
et_text.append(text.get(0));
I guess when you use your code for the first time ,the EditText is empty,then your code executes and set some text after recognizing some speech,on second run you again want to show the text from the speech and want it to be appended to the last text which was set on the EditText.Before setting text on the EditText using setText(),first call getText() on the EditText,and save it in some String variable.Then concatenate the text generated during second run of the code.
if(et_text.getText.length()>0)
{
et_text.setText( et_text.getText().toString() + text.get(0));
}
else
{
et_text.setText(text.get(0));
}
}

How do you populate an edittext field instead of a listview when using speech recognition?

I am using the code from google's sample voice recognition class. They write the top 5 results into a listview but I just want the top result posted in an edittext field. Is this possible? Or is it possible to populate the listview but then automatically copy the results to an edittext field?
Any help would be appreciated.
If you only want 1 result back, you should specify it in the intent you use to start the Voice Recognition Activity:
private void startVoiceRecognitionActivity() {
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);
//since you only want one, only request 1
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, 1234);
}
And then pull the single result and set it to your EditText:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
//pull all of the matches
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String topResult = matches.get(0);
EditText editText = findViewById(R.id.yourEditText);
editText.setText(topResult);
}
}
public void doSome(View v) {
Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Up");
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
startActivityForResult(i, 1111);
//finish();
//Toast.makeText(getApplicationContext(),"Anonymus class 1",Toast.LENGTH_LONG).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == 1111 && resultCode==RESULT_OK){
ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String s=results.get(0);
EditText ed=(EditText)findViewById(R.id.editText);
ed.setText(s);
}
i've used onclick attribute of the button but it doesn't matters this is working just fine in my case

Categories

Resources