Android Text to Speech Tutorial

Text To Speech (TTS) in Android is one of the amazing features which allows to convert your text into voice. Android also allows to speak text in different languages. A Text-to-speech synthesizer is an application that converts text into spoken word, by analyzing and processing the text using Natural Language Processing. In this tutorials, we’ll learn how to implement Text To Speech Android app with a Flat User Interface design that can receive the text or take input as text and read them for the user.

   DOWNLOAD CODE   

Creating Android Text to Speech 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 ⇒ Text-To-Speech
  • 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_bg"
    tools:context="com.gadgetcreek.text_to_speech.MainActivity">

        <TextView android:text="@string/tts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview"
            android:textSize="35sp"
            android:textColor="@color/color_tts"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <TextView android:text="@string/gc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview1"
            android:layout_below="@+id/textview"
            android:layout_margin="30dp"
            android:textSize="25sp"
            android:layout_centerInParent="true"
            android:textColor="@color/color_gc"/>
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_below="@+id/textview1"
            android:layout_marginTop="20dp"
            android:hint="@string/hint"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textColor="@color/color_et"
            android:textColorHint="@color/color_et_hint" />

        <TextView android:text="@string/pitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview2"
            android:textSize="30sp"
            android:textColor="@color/color_pitch"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/editText"/>

        <SeekBar
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/seekpitch"
            android:layout_below="@+id/textview2"
            android:layout_marginTop="20dp" />

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

</RelativeLayout>

Image Link for Speaker/App Icon

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

<resources>
    <string name="app_name">Text-To-Speech</string>
    <string name="tts">Text To Speech</string>
    <string name="gc">Gadget Creek</string>
    <string name="hint">Enter Text</string>
    <string name="pitch">Pitch</string>
</resources>

 and res/values/colors.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="color_bg">#ffffff</color>
    <color name="color_tts">#000000</color>
    <color name="color_gc">#009871</color>
    <color name="color_et">#000cff</color>
    <color name="color_et_hint">#ff0000</color>
    <color name="color_pitch">#000cff</color>
</resources>

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

package com.gadgetcreek.text_to_speech;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextToSpeech tts;
    EditText edittext;
    ImageButton imgbutton;
    SeekBar seekPitch;
    double pitch=1.0;

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

        edittext=(EditText)findViewById(R.id.editText);
        imgbutton=(ImageButton)findViewById(R.id.imgbutton);
        seekPitch = (SeekBar) findViewById(R.id.seekpitch);
        seekPitch.setThumbOffset(5);

        edittext.setText("Welcome to Gadget Creek");

        seekPitch.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                pitch = (float) progress / (seekBar.getMax() / 2);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        
        tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {

                if (status == TextToSpeech.SUCCESS) {
                    if (tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                        tts.setLanguage(Locale.US);
                } else if (status == TextToSpeech.ERROR) {
                    Toast.makeText(getApplicationContext(), "Sorry! Text To Speech failed...",Toast.LENGTH_LONG).show();
                }
            }
        });

        imgbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = edittext.getText().toString();
                tts.setPitch((float) pitch);
                tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }
}

Changing Language:

You can change the language to speak by using setLanguage() function. Lot of languages are supported like Hindi, Canada, French, Chinese, Germany etc.,

With this, we have completed the Text To Speech android application part. To run the app from Android studio, click Run icon Eclipse Run Iconfrom the toolbar. Now just Enter specify Text on the Input Text field and press the speaker button, to launch get the spoken word.

Please comment below with any questions or doubt.

1 thought on “Android Text to Speech Tutorial”

  1. Magnificent web site. A lot of helpful info here. I am sending it to several friends ans also sharing in delicious. And obviously, thank you on your effort!|

Leave a Reply

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