Text To Speech | Android Studio | Kotlin

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 Java

VIDEO:


Step 1: Create a new Project or open new 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.kt
package com.technifysoft.myapplication

import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import java.util.Locale

class Main1Activity : AppCompatActivity() {

    companion object {
        private const val TAG = "MAIN_TAG"
    }
    
    private lateinit var textEt: EditText
    private lateinit var speakBtn: MaterialButton
    private lateinit var stopBtn: MaterialButton

    //Text To Speech
    private lateinit var textToSpeech: TextToSpeech

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

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

        textToSpeech = TextToSpeech(this) { 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 {
            //get text from edit text
            val toSpeak = textEt.text.toString().trim { it <= ' ' }
            if (toSpeak.isEmpty()) {
                //if there is no text in edit text
                Toast.makeText(this@Main1Activity, "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.
                val locale = Locale("en")
                //set locale as language
                textToSpeech.setLanguage(locale)
                //check if language is available
                val result = textToSpeech.isLanguageAvailable(locale)
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    //Language is not Available/Supported
                    Toast.makeText(this@Main1Activity, "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 {
            if (textToSpeech.isSpeaking) {
                //Speaking, stop
                textToSpeech.stop()
            } else {
                //Not speaking
                Toast.makeText(this@Main1Activity, "Not Speaking", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

Step 3: Run Project

Output
Text To Speech - Android Studio - Kotlin
Text To Speech - Android Studio - Kotlin

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)