How to add Text to speech (TTS) in android app using Android Studio

0 1,186

Text to speech (TTS)

In this tut i will show you how you can add Text to speech (TTS) in android app,you can convert your text into voice and also android Provide facility to convert and speak text in variety of different languages.TextToSpeech class used for this purpose,you need to instantiate an object of this class and also specify the initListnere.

Main activity

package exceptionbound.com.howtoaddtexttospeachinandroid;

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

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
    TextToSpeech textToSpeech;

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

        textToSpeech= new TextToSpeech(MainActivity.this,MainActivity.this);
        final Button Spkbutton = (Button) findViewById(R.id.button);
        final TextView textView = (TextView) findViewById(R.id.textView);

        Spkbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!textToSpeech.isSpeaking()){
                    HashMap<String,String> stringStringHashMap = new HashMap<String, String>();
                    stringStringHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"Hello how are you");
                    textToSpeech.speak(textView.getText().toString(),TextToSpeech.QUEUE_ADD,stringStringHashMap);
                    Spkbutton.setVisibility(Button.GONE);
                }else{
                    textToSpeech.stop();
                }
            }
        });

    }

    @Override
    public void onInit(int i) {
        textToSpeech.setOnUtteranceCompletedListener(this);

    }

    @Override
    public void onUtteranceCompleted(String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,"Utterence completed",Toast.LENGTH_LONG).show();
                Button button = (Button) findViewById(R.id.button);
                button.setVisibility(Button.VISIBLE);
            }
        });{

        }

    }

    protected void onDestroy(){
        if (textToSpeech!=null){
            textToSpeech.stop();
            textToSpeech.shutdown();
            textToSpeech=null;
        }
        super.onDestroy();
    }

}

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="exceptionbound.com.howtoaddtexttospeachinandroid.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello how are you"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="104dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="16dp" />

    <Button
        android:text="Speak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView" />
</RelativeLayout>

Download Source code

Comments

comments

Leave A Reply

Your email address will not be published.