How to integrate Android Speech To Text?


Android supports Google inbuilt text to speak API using RecognizerIntent.ACTION_RECOGNIZE_SPEECH. In this example demonstrate about how to integrate Android speech to text.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <LinearLayout
      android:layout_width = "match_parent"
      android:gravity = "center"
      android:layout_height = "match_parent">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"/>
   </LinearLayout>
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_alignParentBottom = "true"
      android:layout_centerInParent = "true"
      android:orientation = "vertical"
      android:layout_height = "wrap_content">
   <ImageView
      android:id = "@+id/speak"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:background = "?selectableItemBackground"
      android:src = "@android:drawable/ic_btn_speak_now"/>
   </LinearLayout>
</RelativeLayout>

In this above code we have created one text view and image view. When user click on image view, it will call Google speech to text API and added text to text view.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
   private final int REQ_CODE = 100;
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.text);
      ImageView speak = findViewById(R.id.speak);
      speak.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());
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak");
            try {
               startActivityForResult(intent, REQ_CODE);
            } catch (ActivityNotFoundException a) {
               Toast.makeText(getApplicationContext(),
               "Sorry your device 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: {
            if (resultCode = = RESULT_OK && null ! = data) {
               ArrayList result = data
               .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
               textView.setText(result.get(0));
            }
            break;
         }
      }
   }
}

In the above code when user click on imageview, it will call intent as shown below -

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, "Need to speak");
try {
   startActivityForResult(intent, REQ_CODE);
} catch (ActivityNotFoundException a) {
   Toast.makeText(getApplicationContext(), "Sorry your device not supported", Toast.LENGTH_SHORT).show();
}

In the above code, we are called Google API and will get result on onActivityResult() as shown below -

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   switch (requestCode) {
      case REQ_CODE: {
         if (resultCode = = RESULT_OK && null ! = data) {
            ArrayList result = data
            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            textView.setText(result.get(0));
         }
         break;
      }
   }
}

In the above code, we will get the result as Array List so we are taking zero position from array list and appending to text view. 

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

When user click on the mic button, it will call Google API as shown below -

Now we have given input as "Hey GOOGLE". It will append the result as shown below -

Click here to download the project code

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements