Android Speech Recognition Example

Getting user input via keyboard is OK, but enabling user to just speak rather than having to type is 21st century. Today we will look at Google Speech Recognition and try to integrate it in our android application. It asks user to speak what they intend to enter. It then takes that audio input from user to server, processes it, and returns us output in String format.

CREATE A LAYOUT

Lets first create a button in our main layout.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak up!"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

And a TextView to display the result.


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" "
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

CHECK IF INTERNET CONNECTED

On this button click, we will first check if Internet is connected. If connected, we move ahead to bring our speech recognizer up.

 speakUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isInternetConnected()) {
                 // bring our speech recognizer
                } else {
                    Toast.makeText(getApplicationContext(), "Internet is not connected!", Toast.LENGTH_SHORT).show();
                }
            }
        });

We check if Internet is connected or not with following snippet of code:

public boolean isInternetConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net = cm.getActiveNetworkInfo();
        if (net != null && net.isAvailable() && net.isConnected()) {
            return true;
        } else {
            return false;
        }
    }

SUMMON GOOGLE SPEECH RECOGNIZER

If we are connected to Internet, let the show begin!

if (isInternetConnected()) {
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    startActivityForResult(intent, REQUEST_CODE);
                } else {
                    Toast.makeText(getApplicationContext(), "Internet is not connected!", Toast.LENGTH_SHORT).show();
                }

We are starting speech recognition activity but we need to get output from that activity into our activity so we have used startActivityForResult(). The REQUEST_CODE is a variable which holds an integer defined in starting. When speech recognition is done doing what it does best, we can retrieve result in our onActivityResult() method.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            result_tv.setText(data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0).toString());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Notice that we are displaying first result returned in data Intent. Actually, it returns suggestions according to what user speaks. We can display these results in a dialog and ask user to select most appropriate result. We are not getting into it. Check out full source code here on Github.

See it in action:

Android Speech Recognition example in action

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s