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));
}
}
Related
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.
I am trying to hide the speech recogniser dialog that shows up saying "speak now" and written google as the title ,whenever i call it for passing some voice command. I don't want it to show up. Instead I want it to work in teh background so that the user cannot see it. What should be the correct way to do it?
public void getSpeechInput(View view) {
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
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(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, 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);
txvResult.setText(result.get(0));
String str = txvResult.getText().toString();
if(str.equals("lights on")){
Toast.makeText(MainActivity.this,
"matched", Toast.LENGTH_LONG).show();
count[0]=1;
turnOnFlash();
}
else if(str.equals("lights off")){
Toast.makeText(MainActivity.this,
"didnt", Toast.LENGTH_LONG).show();
count[0]=0;
turnOffFlash();
}
}
break;
}
}
I tried using this but it didn't work:-
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
Try this. This listener catches the events and you can process them accordingly.
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(new RecognitionListener()
{
//Do whatever you want
});
I'm making new App and i need to use voice recognition to write the words that the user said and do some algorithmic on them, What API's shall i use or a library ?
Thanks
Following two method is enough for basic understanding of Speech to text conversion:
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();
}
}
/**
* Receiving speech input
* */
#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);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
I can share a complete project which is available online as well.
Download the sample project from here.
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.
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