Android Text to Speech Tutorial

In this android tutorial we are going to learn creating Android Text to Speech. Android allows you convert your text into voice. Not only you can convert it but it also allows you to speak text in variety of different languages.

Android Text to Speech Tutorial

Download projct file of  Android Text  To Speech  Tutorial – Android Studio

I developed a simple interface with one input field and a button to trigger a event that will take text from input field and speaks out.

texttospeech

1. Create a new project by going to File ⇒ New Android Project. and fill required details.

2. Now add following code in src/MainActivity.java.

package com.andrious.texttospeech;

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 android.widget.ImageButton;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextToSpeech t1;
    EditText write;
    ImageButton speakbtn;


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

        write=(EditText) findViewById(R.id.editText);
        speakbtn=(ImageButton)findViewById(R.id.board);

        t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });
        speakbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = write.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });

    }
    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onDestroy();
    }
}

3. Now add following xml in activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:gravity="center"
 android:background="#3F51B5"
 android:padding="10dp"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <EditText
     android:id="@+id/editText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="5dp"
     android:background="#ffffff"
     android:padding="8dp"
     android:inputType="textMultiLine"
     android:maxHeight="140dp"
     android:scrollbars="vertical"
     android:hint="Write your text here :"/>
 <ImageButton
     android:layout_gravity="end"
     android:layout_width="90dp"
     android:layout_height="60dp"
     android:shadowRadius="5"
     android:id="@+id/board"
     app:srcCompat="@drawable/speaker"
     android:scaleType="fitXY"/>
</LinearLayout>

4. Now run your project and test your app by entering some text in input filed.

Changing Language

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

text to speech

t1.setLanguage(Locale.UK); // UK language

Changing Pitch Rate

You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.

t1.setPitch(0.6);

Changing Speed Rate

The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5

t1.setSpeechRate(2);

 

Leave a Reply