Android Text To Speech Tutorial
Android Sample Codes

Android Text To Speech Tutorial

Android Text to speech is an integral part of mobile application development. In this tutorial, we will learn how “Text To Speech (TTS)” feature can be implemented in an Android APP.

Android allows you convert your text into voice. Android also allows you to speak text in variety of different languages.

Android provides TextToSpeech class for convert text into voice. In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below –

TextToSpeech t1;t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
            t1.setLanguage(Locale.UK);
        }
    }
});

Language can be set by calling setLanguage() method. Its syntax is given below –

t1.setLanguage(Locale.UK);

The Locale can be as follows

US

CHINA

CANADA_FRENCH

ITALY

GERMANY

JAPAN

 

Once you have set the language, call speak method of the class TextToSpeech to speak the text. Its syntax is given below –

t1.speak(str, TextToSpeech.QUEUE_FLUSH, null);

Text to speech makes an android device read the text from the user and convert it to audio out via the speaker. As we discussed Android text to speech supports multiple languages.

Android Text To Speech App Demo

In order to learn how it works, we are going to build a simple Andriod App with a EditText and a button and so that the user can input text. Up on user click on the button the Android APP will convert the text to speech or audio.

Create a new project for Text To Speech app by select File>New>New Project>Empty Activity in the android studio.

Let’s start by designing the layout of our example Android App.

activity_main.xml

There are two Views created in this Text to Speech tutorial. First is a an EditText which will receive text input from the user. Second one is the button on click of which the app reads the user inputted text in EditText and calls the Android text to speech API to convert the user entered text  to speech.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="Enter Text for Speech Conversion"
        android:id="@+id/et1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="Click To Convert Text To Speech"
        android:onClick="onTextToSpeech"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Android Text To Speech Demo"
        android:textColor="@color/colorAccent"
        android:textSize="50dp"/>
</LinearLayout>

MainActivity.java

In this implementation of Text to Speech conversion we have to instatntiate object of TextToSpeech class in android and this is the Android class that does the job of converting the text to speech and also provides parameters to set the language, speed and pitch.

package com.example.speechapp;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    TextToSpeech t1;
    EditText ed1;
    Button b1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });

    }
    public void onTextToSpeech(View v)
    {
        ed1 = (EditText)findViewById(R.id.et1);
        String str = ed1.getText().toString();
        t1.speak(str, TextToSpeech.QUEUE_FLUSH, null);

    }
}

Here is the content of AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

Output :

Android Text To Speech Tutorial

We are done! Now you can run and demo the Android app and convert your text to speech.

Leave a Reply

Your email address will not be published. Required fields are marked *