Android Text to Speech Example

In this tutorial you will learn about android text to speech example.

Android provides a class TextToSpeech that helps in converting any text into speech or voice. What I am doing in this example is, I am writing any text in textbox and on button click I am taking the text from textbox and converting it into speech using TextToSpeech class.

Android Text to Speech Example

First we need to create the object of TextToSpeech class. Its constructor takes two arguments, first argument is the reference of current activity and in the second argument we have to specify the listener. The setLanguage() method is used to specify the language, it takes Locale object as argument. The Locale object can be US, UK, CHINA, etc. Finally the speech() method is used to convert the text to speech, it takes three parameters or arguments. The first argument is the text that we want to convert, second argument takes constant QUEUE_FLUSH and third argument takes null.

Below I have given the whole code for doing this. The package name is com.texttospeech.

activity_main.xml

<LinearLayout 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" android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text To Speech"
        android:id="@+id/btn"/>

</LinearLayout>

 

MainActivity.java

package com.texttospeech;

import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;


public class MainActivity extends Activity {
    EditText text;
    Button btn;
    TextToSpeech tts;

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

        text=(EditText)findViewById(R.id.text);
        btn=(Button)findViewById(R.id.btn);

        tts=new TextToSpeech(MainActivity.this,new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status!=TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.US);
                }
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tts.speak(text.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
            }
        });
    }
}

 

AndroidManifest.xml

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

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

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

</manifest>

 

Android Text to Speech Example

 

So this was the simple tutorial for android text to speech example. You can comment below if you have any doubts regarding above tutorial.

2 thoughts on “Android Text to Speech Example”

Leave a Comment

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