Pawan Kumar
Pawan Kumar

Reputation: 1533

How to add a Speech recognition button in my Android App?

I am a Java Developer. Sorry I have 0 Idea on Android. Please bear with me.

I am thinking of creating an app. To build my app first I must learn must learn android. To get the motivation I am trying to Sketch the app, how it should look like. I thought of making my app hands free, meaning that I want to use Speech Recognition for navigation and performing all the tasks.

Now my question is "Is there any way I can add the Google's Speech recognizer to my App as a Custom Button?" So it will not take half the screen it will just listen at the end of the screen. I will convert the audio into a text file so, I can use API.AI to do some stuff behind it.

Or there any other ways to achieve my goal?

Can the google speech recognizer work on iOS too?

Upvotes: 0

Views: 884

Answers (1)

Naveen
Naveen

Reputation: 360

You can use android SpeechRecognizer.Check out the link for the implementations: Visit https://developer.android.com/reference/android/speech/SpeechRecognizer.html

I tried SpecchRecognizer in one of my project.

First You have to implement your activity like this

public class MainActivity extends Activity implements RecognitionListener

Then do below code in your OnClickListener.

SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(this);
                 speech.setRecognitionListener(this);
                 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");
                 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
                 speech.startListening(intent);

In OnActivityResult You can get the result.

ArrayList<String> result = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
textview.setText(result.get(0));

You can also get below implemenations.you can do whatever you want in the below override methods.

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int error) {

}
@Override
public void onReadyForSpeech(Bundle params) {

}

That's all.

Upvotes: 1

Related Questions