Text To Speech - Android Studio - Java

In this video we will create a "Text To Speech" application, which can be used to speak the text from any view e.g. EditText, TextView etc.
>>Watch For Kotlin:

VIDEO:


Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".MainActivity">

    <!--EditText in which we will input text to speak-->
    <EditText
        android:id="@+id/textEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:gravity="start"
        android:background="@drawable/bg_edittext"
        android:padding="5dp"
        android:hint="Enter Text to speak..."/>

    <!--Button: on click start reading content of EditText-->
    <Button
        android:layout_below="@id/textEt"
        android:drawableLeft="@drawable/ic_speak"
        android:drawablePadding="5dp"
        android:id="@+id/speakBtn"
        android:text="Speak"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--Stop speaking button-->
    <Button
        android:layout_below="@id/textEt"
        android:drawableLeft="@drawable/ic_stop"
        android:layout_alignParentEnd="true"
        android:drawablePadding="5dp"
        android:id="@+id/stopBtn"
        android:text="Stop"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />


</RelativeLayout>

MainActivity.java
package com.blogspot.atifsoftwares.texttospeech_java;

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.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    //views
    EditText mTextEt;
    Button mSpeakBtn, mStopBtn;

    TextToSpeech mTTS;

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

        mTextEt = findViewById(R.id.textEt);
        mSpeakBtn = findViewById(R.id.speakBtn);
        mStopBtn = findViewById(R.id.stopBtn);

        mTTS = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR){
                    //if there is no error then set language
                    mTTS.setLanguage(Locale.UK);
                }
                else {
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //speak btn click
        mSpeakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get text from edit text
                String toSpeak = mTextEt.getText().toString().trim();
                if (toSpeak.equals("")){
                    //if there is no text in edit text
                    Toast.makeText(MainActivity.this, "Please enter text...", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this, toSpeak, Toast.LENGTH_SHORT).show();
                    //speak the text
                    mTTS.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                }
            }
        });

        //stop btn click
        mStopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTTS.isSpeaking()){
                    //if it is speaking then stop
                    mTTS.stop();
                    //mTTS.shutdown();
                }
                else {
                    //not speaking
                    Toast.makeText(MainActivity.this, "Not speaking", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onPause() {
        if (mTTS != null || mTTS.isSpeaking()){
            //if it is speaking then stop
            mTTS.stop();
            //mTTS.shutdown();
        }
        super.onPause();
    }
}

Step 3: Run Project

Output
Text To Speech - Android Studio - Java
Text To Speech - Android Studio - Java

Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)