Tutorials Android Convert Speech to Text in Android Application

Convert Speech to Text in Android Application

Many of the cool Android features are remained untouched by app developers. For example, Android SDK has the capabilities to seamlessly integrate the user speech recognition services to turn speech to into text. Apps like Google Keep, Any.DO uses speech to text conversion feature quite creatively. In today’s world, big players like Google, Microsoft, and Apple are betting big on voice assistance software like Google voice command, Cortana, and Siri. This shows how serious and powerful the feature is.

Android natively provides converting Text to Speech and Speech to Text feature, so why not to use it in our app! In this tutorial, we will show you how to integrate Android’s Speech to Text API in your Android app. This simple application will record the user’s voice, convert it into text and display on Android Screen.

As soon as a user say something, Android will recognize his/her voice and convert it into text. It will do it through RecognizerIntent. You do not require any internet connection to use the voice to text service. It will work in Offline mode.

Without much of a fuss, let us now proceed with the example.

Pre-requisites:

  • Android Studio IDE on your PC or Mac.
  • This App doesn’t support Emulator. So you need an Android Smartphone or Tablet.
  • Basic knowledge of Android app lifecycle, knowledge of building UI layouts.

Also read, how to Connect, Configure App on Android device and Run Hello World program with Android Studio.

Create New Android Project

  1. Open Android Studio and create a new project Speech to Text and company domain application.example.com (We have used our own company domain i.e stacktips.com).
  2. Click Next and choose Min SDK, we have kept the default value. Again Click Next and Choose Blank Activity.
  3. Choose the Activity as MainActivity and click next.
  4. Leave all other things as default and Click Finish.

A new project will be created and gradle will resolve all the dependencies.

Activity Layout

Let us add an ImageButton representing a mic, and a TextView to display the converted text in it. The TextView is wrapped inside a ScrollView for allowing longer scrollable content.

activity_main.xml

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.stacktips.speechtotext.MainActivity">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnSpeakContainer"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="20dp"
        android:padding="20dp">

        <TextView
            android:id="@+id/voiceInput"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </ScrollView>

    <LinearLayout
        android:id="@+id/btnSpeakContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#f5f5f5"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="20dp">

        <ImageButton
            android:id="@+id/btnSpeak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:padding="16dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_microphone_2" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnSpeak"
            android:layout_margin="10dp"
            android:text="@string/hint" />
    </LinearLayout>
</RelativeLayout>

Inside Android Activity

This is the main part of voice recognition app where we will user RecognizerIntent to convert speech into text. Add following code in MainActivity.java:

MainActivity.java

package com.stacktips.speechtotext;

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

public class MainActivity extends AppCompatActivity {

    private static final int REQ_CODE_SPEECH_INPUT = 100;
    private TextView mVoiceInputTv;
    private ImageButton mSpeakBtn;

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

        mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
        mSpeakBtn = (ImageButton) findViewById(R.id.btnSpeak);
        mSpeakBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startVoiceInput();
            }
        });
    }

    private void startVoiceInput() {
        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, "Hello, How can I help you?");
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {

        }
    }

    @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);
                    mVoiceInputTv.setText(result.get(0));
                }
                break;
            }

        }
    }
}

In the above code, we are triggering an Intent named RecognizerIntent which asks for speech input and then sends it through speech recognizer. It does it through ACTION_RECOGNIZE_SPEECH. If request code is REQ_CODE_SPEECH_INPUT then corresponding text is written in output screen.

Let us now build and run this example.

After you tap the Mic button, second screen will appear that will ask you to speak something. Now if you speak anything, it will be shown on screen.
Speech to Text in Android
Also read, how to integrate Text to Speech converter in your Android application.

Download Source Code

[download url=”https://github.com/StackTipsLab/Android-Speech-to-Text-Example”]

We hope, this tutorial was helpful for you to in integrating Speech to Text in your Android app. Join the discussion and leave a comment, in the case of any doubts.

Total
1
Shares
guest
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Varun Chandran Vaikom
Varun Chandran Vaikom
5 years ago

Sir I want help with my project. An English to Hindi translator with voice input output.it need to hav a database containing common phrases in those languages.the input is searched in db Ann corresponding phrase in other lang is reetturned. Tnx in advance

Ankit sahu
Ankit sahu
4 years ago

Hi did u find any help regarding this idea?

Derek Welsh
Derek Welsh
4 years ago

Hi Navneet, i ran the application up a Galaxy note 10.1 and it works very nicely. However, once the tablet is switched to flight mode or removed from the network, the application no longer provides an ability to record speech. Your thoughts ?

Derek Welsh
Derek Welsh
4 years ago

Apologies, it was my tablet 🙂

Ram Ramesh
Ram Ramesh
4 years ago

Great tutorial.. How do I run this in the background and when screen is locked.. Is it possible to run it as a service so it is continuously looking for a keyword?
Thanks
Ramesh

Luis M
Luis M
4 years ago

Thanks, This is Amazing, Can i use the code freely?

deepak
deepak
4 years ago

thanx Navneet ,
its perfectly working

Adam
Adam
4 years ago

To know the reason that how to run this app in the background and when the screen is locked. For that, you must know some kind of the following things which mainly possess from the Android support number and from there you can easily get to know that from there.

dell support
dell support
4 years ago

We can change the human voice to the text using the Android Studio. Using the Google speed library, the speech can easily be changed to the text. We can implement this to this Android Studio.

Anish subedi
Anish subedi
4 years ago

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()) doesn’t support Hindi language at this line why ?

akhil
akhil
4 years ago
Reply to  Anish subedi

u need to change the default language in your phone settings to Hindi then it will work

Aalishan
Aalishan
3 years ago
Reply to  akhil

how to set specific one language like hindi instead of get default language

TheGameAppStudio
TheGameAppStudio
4 years ago

Hi,So after adding one text how can i add next text along with previous text in same line with space?

Waqas Nabi
Waqas Nabi
3 years ago

How we will add Language options? as you can check other apps , they have a dropbox to select the language. Can you explain it plz?

rmanjunath000
rmanjunath000
3 years ago

Hello Sir,
Can we change “app name” instead of “Google” in voice search

shabbir bhai
shabbir bhai
2 years ago
Reply to  rmanjunath000

in manifest file you can change

flashscribe ai
flashscribe ai
2 years ago

Great! Thanks

Previous Post
Google Fabric

Google Acquires Fabric to Expand Footprint on App Analytics

Next Post

Real Time Is Greater than User and Sys Time

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
17
0
Would love your thoughts, please comment.x
()
x