Comparing speech input on Android with a word - android

I made this to check if what people say contain "recherche" but it doest display the toast :
public void bn6(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_PROMPT, "Speech to text");
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
TextView speechText = (TextView)findViewById(R.id.speechText);
speechText.setText(matches.get(0).toString());
String spec = speechText.getText().toString();
if (spec.toLowerCase().contains("Recherche")) {
Toast.makeText(getApplicationContext(), "bravo", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
so it changes the textview but it doesn't toast anything

spec.toLowerCase().contains("recherche")
Your text will always be false if you compare lower 'r' to Capital 'R'
Use "recherche" with lower case

In this part :
spec.toLowerCase().contains("Recherche")
you convert spec to lower case but Recherche start with R that is an upper case! use this instead :
spec.toLowerCase().contains("recherche")

Related

"Saving audio to .....#gmail.com" toast when using RecognizerIntent.ACTION_RECOGNIZE_SPEECH

I am using the RecognizerIntent.ACTION_RECOGNIZE_SPEECH to capture audio using the following code:
public void promptSpeechInput() {
if (checkAudioPermission()) {
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 Your Query Now");
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} else {
showShortSnackBar("Record Permission Required");
}
}
and I have implemented the onActivityResult as follows:
#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);
String userQuery = result.get(0);
addMessage(false, "Query: \n" + userQuery);
txtSpeechInput.setText(userQuery);
}
break;
}
}
}
Everything works fine except I get an annoying toast saying:
"Saving audio to .....#gmail.com"
How can I get rid of this toast message?

Convert pre-recorded file into Text using Google Speech Api in Android

Using google speech Api we can convert Speech to text using given link. But I already have an Audio file and I want that file to be converted into text. Please help. Thank You.
When u click on Speech button that time call
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();
}
#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 think it will help you

How to use voice recognition and auto write the words

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.

Android RecognizerIntent Speech recognition

How to handle the visibility of an image(ImageView) in the event the RecognizerIntent finishes due to the user not speaking
if (RecognizerIntent.EXTRA_RESULTS == null){
image1.setVisibility(View.VISIBLE);///microphone icon
}
or
if (RecognizerIntent.ACTION_RECOGNIZE_SPEECH == null){
image1.setVisibility(View.INVISIBLE);///microphone
}
thnx
In the code above you are just testing if the constants are null, which they are not. You have to start the recognition somewhere in the code by
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//... put other settings in the Intent
startActivityForResult(intent, REQUEST_CODE);
and receive the result in
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
//... do your stuf with results
}
super.onActivityResult(requestCode, resultCode, data);
}
A more customizable way is to use SpeechRecognizer directly. See for example this question.

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