Android specific words speech recognition - android

I am trying to have the app recognize certain words said by the user using the code below, but for some reason it isn't working at all. Please review this and tell me what is wrong with it. Thank you
The app is simply suppose to display a toast message if the words "orange" or "apple" is said but nothing happens when using the code below.
//button onclick to trigger RecognizerIntent
public void OnClick_Speed_Detector(View v)
{
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak up");
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 1 && resultCode == RESULT_OK)
{
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(((result).equals("orange")))
{
Toast.makeText(getApplicationContext(), "orange", Toast.LENGTH_LONG).show();
}
else
if (((result).equals("apple")))
{
Toast.makeText(getApplicationContext(), "apple", Toast.LENGTH_LONG).show();
}
}
}

Your issue is that you are testing if an ArrayList == a String, when it can't possibly (an ArrayList of type String contains multiple strings)
Instead of:
if ((result).equals("orange")) {}
Try:
if ((result).contains("orange")) {}
This code will look through every index of the ArrayList and determine if any of the indexes of it equal "orange". If any do then it will return
true
...and it will execute the if statement!
Hope this helps!

Related

How to implement Xamarin Speech Recognition

I am new in xamarin, and I was wondering if it is possible to implement this kind of speech recognition:
First the user inputs "Hello" but the text output will be "Hi"?
I have found this link: Android speech recognition pass data back to Xamarin Forms
but it only outputs the speech "Hello" as a text "Hello".
Speech recognition usually involves translating what you say into text.If you need to change its content, perhaps you can try to make some judgments directly after the transformation and make changes according to your requirements like change the result in the link above :
in MainActivity OnActivityResult:
const int VOICE = 10;
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == VOICE)
{
if (resultCode == Result.Ok)
{
var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
if (matches.Count != 0)
{
var textInput = matches[0];
if (textInput.Length > 500)
textInput = textInput.Substring(0, 500);
//make a judgment and change the value
if(textInput.Eques("hello")){
textInput = "Hi";
}
SpeechToText_Android.SpeechText = textInput;
}
}
SpeechToText_Android.autoEvent.Set();
}
}

Marathi - Speech to Text on Android

Google Speech-to-Text API supports Marathi as per their documentation here. However I have not been able to get it working on my Android phone. I have already added 'Marathi' in languages for my Android device (Moto G6, running android 7.1.1). However, I am not yet able to get a simple SMS converted from speech-to-text with this. Marathi typing works fine though.
Do I need to modify any other setting? What else is required? Any pointers for this would be highly appreciated.
Have you configured your intent to detect marathi languages as below?
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"mr-IN");
You can checkout other languages codes here.
You can use following code for your reference:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.RecordBtn:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Use Off line Recognition Engine only...
intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, false);
// Use Marathi Speech Recognition Model...
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "mr-IN");
try {
startActivityForResult(intent, SST_REQUEST_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.error),
Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SST_REQUEST_CODE:
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> getResult = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Original.setText(getResult.get(0));
conversionTable = new ConversionTable();
String Transformed_String = conversionTable.transform(getResult.get(0));
result.setText(Transformed_String);
}
break;
}
}

Android Making a Call Only Works the Second Time

So I'm making a Speech to Text app using the voice assistant. I'm trying to make a phone call feature so the user can speak a number and it will call it.
I'm almost there but the number only rings the second time I speak it. The first time it says "Call not sent".
I figured out the reason for this is; when the user speaks the number it's not updating the variable first and then calling the "call" function. I've tried almost everything but it doesn't update the variable correctly.
I.e.
private TextView txtSpeechInput;
public String num = "123";
#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).replaceAll("\\s+", ""));
num = txtSpeechInput.getText().toString();
}
break;
}
}
}
public void dialPhoneNumber(String phone) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phone));
if (intent.resolveActivity(getPackageManager()) != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}
return ;
}
}
private void processResult(String command) {
command = command.toLowerCase();
if(command.indexOf("time") != -1) {
Date now = new Date();
String time = DateUtils.formatDateTime(this, now.getTime(), DateUtils.FORMAT_SHOW_TIME);
speak("The time is " + time);
}
if(command.indexOf("date") != -1) {
String date = DateFormat.getDateInstance().format(new Date());
speak("The date is " + date);
}
else if (command.indexOf("open") != -1) {
if(command.indexOf("browser") != -1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.uk/"));
startActivity(intent);
}
}
if(command.indexOf("call") != -1) {
promptSpeechInput();
try {
Thread.sleep(18000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialPhoneNumber(num);
}
}
In this code, wen the use says "make a call" it opens another prompt to take the speech input. Stores it in txtSpeechInput (Where it says results.get0) and then at that stage I update the "num" variable and convert it to a string.
It then runs dialPhoneNumber
Now let's say I run it the first time and speak "07123456789", it will say call not sent because its trying to call the default 123, if i speak it again or a different number then it will ring the 07123456789.
How and why does it not update before calling the phone feature?
Based on danny117's comment...
promptSpeechInput();
try {
Thread.sleep(18000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialPhoneNumber(num);
promptSpeechInput starts a new activity, and the result from that activity is the spoken text? If that's right, then why are you prompting for input, sleeping for 18 seconds (never sleep on the main thread by the way), and then assuming your input is ready? As danny says, prompting for input should be the last thing that if block does. Dialing the number should be initiated from onActivityResult.
Also, why is there a default phone number of "123"? That will never be correct, so is not a sensible default. And to reiterate, if you are putting the thread to sleep for a fixed amount of time waiting for something else to happen, you're almost certainly approaching it the wrong way. And if you're putting the main thread to sleep in Android you're absolutely doing it the wrong way.

How to call onClick multiple times in the same activity?

UPDATED:
I am creating an app where onClickListener is used to convert speech to text and input the text to a List View field. Likewise once entered I would like to re-trigger the speech to text option and input the new text into another List View field whilst still retaining all the other List View fields which were already filled in.
The following is part of the Java file that calls the speech to text option with the various statements I want answered. Originally the prompts appeared exactly one after the next which is perfect but it didn't assign each text to the corrresponding edit text field. It only recorded the speech to text of the very last prompt only. Now with some help on this thread I have updated the code to the following, where checklv1 etc correspond to unique integers:
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the current time?");
startActivityForResult(i1, check);
Intent i2 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i2.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i2.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the status Confirmed or Unconfirmed?");
startActivityForResult(i2, checklv1);
Intent i3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i3.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i3.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the Temp?");
startActivityForResult(i3, checklv2);
}
The following part of code shows to assign the text to the editText field lv1, lv2 and lv3:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv1 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv2 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv3.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
}
The code is now progressing and inputting the speech to text phrases into each of the list view fields. However my only issue is that lv3 contains the correct phrase, lv1 also contains the correct phrase but lv2 is a the same phrase as lv1 when it should be the phrase corresponding to lv2. Most likely there is something wrong in the check assignment.
If anyone can please help me with where I have gone wrong in the above code that will be greatly appreciated
The check field you are passing to startActivityForResult is what you should be using to distinguish the action you are performing. Pass 3 different values here and switch on them in onActivityResult to determine which part of the UI you want to update.
startActivityForResult(i2, checkLv1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode) {
case checkLv1:
update(lv1);
...

Making the device speak text on command

I am having difficulty in framing this question but anyways here it goes, I have made two applications basically the TextToSpeech app and the SpeechToText application. They are working fine, now I am trying to merge them. Basically I want the the text to speech part to say the text entered in a text field , only if the user says the word "speak".That would be done using the speech to text part. Now the problem I am having is that android displays a list of outputs when the user says something, but I only one output that is "Speak" . How can I get only one output instead of a list.
Initially the app becomes active on button click.
The code I am trying is as follows:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "system Activated");
startActivityForResult(i, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == check && resultCode == RESULT_OK) {
String command = data
.getStringExtra(RecognizerIntent.EXTRA_RESULTS).toString();
if (command.equals(command_verify)) {
speak();
} else
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
public void speak() {
String text_to_speak = message_field.getText().toString();
talk.speak(text_to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
The Voice Recognizer returns you a list of guesses, where the first guess is the most accurate. You can use it calling list.get(0). Hope this helps.

Categories

Resources