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"?>
<RelativeLayout 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:padding="5dp"
    tools:context=".MainActivity">

    <!--EditText: whose text to be spoken-->
    <EditText
        android:id="@+id/textEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:padding="10dp"
        android:hint="Enter text to speak..."
        android:gravity="start"
        android:background="@drawable/bg_edittext"/>

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

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

</RelativeLayout>

MainActivity.kt

package com.blogspot.atifsoftwares.texttospeech_kotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    //Text To Speech
    lateinit var mTTS:TextToSpeech

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

        mTTS = TextToSpeech(applicationContext, TextToSpeech.OnInitListener { status ->
            if (status != TextToSpeech.ERROR){
                //if there is no error then set language
                mTTS.language = Locale.UK
            }
        })

        //speak button click
        speakBtn.setOnClickListener {
            //get text from edit text
            val toSpeak = textEt.text.toString()
            if (toSpeak == ""){
                //if there is no text in edit text
                Toast.makeText(this, "Enter text", Toast.LENGTH_SHORT).show()
            }
            else{
                //if there is text in edit text
                Toast.makeText(this, toSpeak, Toast.LENGTH_SHORT).show()
                mTTS.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null)
            }
        }

        //stop speaking button click
        stopBtn.setOnClickListener {
            if (mTTS.isSpeaking){
                //if speaking then stop
                mTTS.stop()
                //mTTS.shutdown()
            }
            else{
                //if not speaking
                Toast.makeText(this, "Not speaking", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onPause() {
        if (mTTS.isSpeaking){
            //if speaking then stop
            mTTS.stop()
            //mTTS.shutdown()
        }
        super.onPause()
    }
}

Step 3: Run Project

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

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)