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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text To Speech"
        android:textSize="20sp" />

    <!--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:layout_marginVertical="20dp"
        android:gravity="start"
        android:hint="Enter Text to speak..."
        android:minHeight="100dp"
        android:padding="5dp" />

    <!--Button: on click start reading content of EditText-->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/speakBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="5dp"
        android:text="Speak" />

    <!--Stop speaking button-->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/stopBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="5dp"
        android:text="Stop" />

</LinearLayout>

MainActivity.java

package com.technifysoft.myapplication;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.button.MaterialButton;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MAIN_TAG";

    private EditText textEt;
    private MaterialButton speakBtn;
    private MaterialButton stopBtn;

    //Text To Speech
    private TextToSpeech textToSpeech;

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

        textEt = findViewById(R.id.textEt);
        speakBtn = findViewById(R.id.speakBtn);
        stopBtn = findViewById(R.id.stopBtn);

        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    // Language is supported, no need for action here
                    Log.d(TAG, "onCreate: Text To Speech is ready...");
                } else {
                    Log.d(TAG, "onCreate: Text To Speech error");
                }
            }
        });

        //handle speakBtn click, speak text
        speakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //get text from edit text
                String toSpeak = textEt.getText().toString().trim();
                if (toSpeak.isEmpty()) {
                    //if there is no text in edit text
                    Toast.makeText(MainActivity.this, "Please enter text...", Toast.LENGTH_SHORT).show();
                } else {
                    //Locale to set as language e.g. "en" for "English", "ur" for "Urdu", "hi" for "Hindi" etc.
                    Locale locale = new Locale("en");
                    //set locale as language
                    textToSpeech.setLanguage(locale);
                    //check if language is available
                    int result = textToSpeech.isLanguageAvailable(locale);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        //Language is not Available/Supported
                        Toast.makeText(MainActivity.this, "Language Not supported", Toast.LENGTH_SHORT).show();
                    } else {
                        //Language is Available/Supported, speak the text
                        textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    }
                }
            }
        });

        //handle stopBtn click, speak text
        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (textToSpeech.isSpeaking()) {
                    //Speaking, stop
                    textToSpeech.stop();
                } else {
                    //Not speaking
                    Toast.makeText(MainActivity.this, "Not Speaking", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

Step 3: Run Project

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

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

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