Android Speech To Text Tutorial

Speech To Text feature which allows to converts the human voice into the written word. In this tutorial, I will show you the conversion of voice commands into text using google speech recognition service on the device that’s registered to receive a RecognizerIntent. When RecognizerIntent is trigger dialog box appears to recognize speech input and streaming audio to Google’s servers, then converts the speech into text and send backs the result to our calling Activity.

   DOWNLOAD CODE   

Creating Android Speech To Text Project:

Refer Android beginners app development guide if you are a beginner or if you don’t know how to create a project in the android studio.

1. In Android Studio, create a new project

  • If you don’t have a project opened, in the Welcome screen, click New Project.
  • If you have a project opened, from the File menu, select New Project.

2. Under Configure your new project, fill  the following fields as given below

  • Application Name ⇒ Speech-To-Text
  • Company domain ⇒ example.com ( Eg: gadgetcreek.com)
  • Project location ⇒ choose a location for your project.

3. Under Select, the form factors your app will run on, check the box for Phone and Tablet.

4. For Minimum SDK, select API 10: Android 2.3.3 (Gingerbread). click Next.

5. Under Add an activity to Mobile, select Empty Activity and click Next.

6. Click the Finish button to create the project.

7. Select your View as Project, Open the layout file for main activity(activity_main.xml) located under src/main/res/layout/activity_main.xml and add below code to create a simple layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background">

        <TextView
            android:text="@string/gc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview1"
            android:layout_margin="30dp"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:textColor="#ffffff"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Text"
            android:layout_below="@+id/textview1"
            android:layout_marginTop="20dp"
            android:text="@string/mic"
            android:textColor="#ffffff"
            android:textSize="25dp"
            android:layout_centerInParent="true"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/mic"
            android:id="@+id/mic"
            android:layout_below="@+id/Text"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true"
            android:background="@null" />

</RelativeLayout>

Image Link for Activity Background and Mic ImageButton.

Following is the content of the res/values/strings.xml.

<resources>
    <string name="app_name">Speech-To-Text</string>
    <string name="gc">Gadget Creek</string>
    <string name="mic">Tap on mic to speak</string>
</resources>

8. Now Open the MainActivity.java file located under src/main/Java/MainActivity.java and add below code.

First we need to create a RecognizerIntent by setting necessary flags such as

  • Call the RecognizerIntent – Your application calls the Voice Recognition Intent that records the voice.
  • ACTION_RECOGNIZE_SPEECH – Starts an activity that will prompt the user for speech and send it through a speech recognizer.
  • EXTRA_LANGUAGE_MODEL – Informs the recognizer that which speech model should be preferred when performing ACTION_RECOGNIZE_SPEECH. It is adapted to input messages in English.
package com.gadgetcreek.speech_to_text;

import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final int RESULT_SPEECH = 1;
    private ImageButton btnSpeak;
    private TextView Text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Text = (TextView) findViewById(R.id.Text);
        btnSpeak = (ImageButton) findViewById(R.id.mic);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault());

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    Text.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),"Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RESULT_SPEECH: {
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    Text.setText(text.get(0));
                }
                break;
            }

        }
    }
}

 

9. Finally, open your AndroidManifest.xml. located under src/main/AndroidManifest.xml and add the INTERNET  permission as we need to make internet calls and RECORD_AUDIO. This is how your manifest file should look like.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gadgetcreek.speech_to_text">

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

With this, we have completed coding work of Speech To Text android application. To run the app from Android studio, click Run icon Eclipse Run Icon from the toolbar.

NOTE:-  Please make sure that you are connected to the internet.

Now just click on mic so that google speech recognition will popup and speak some command to convert them into written word.

Please comment below with any questions or doubt.

1 thought on “Android Speech To Text Tutorial”

  1. Hello There. I discovered your blog the usage of msn. That is a really neatly written article. I will make sure to bookmark it and return to read more of your useful information. Thank you for the post. I’ll definitely return.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.