How to Create Text to Speech App in Android Studio Convert Text into Voice or Speak Text

How to Create Text to Speech App in Android Studio Convert Text into Voice or Speak Text




Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.








Text To Speech - Android Studio - Java








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 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/ic_baseline_edit_24"
        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_baseline_mic_24"
        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_baseline_stop_24"
        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.usmtip.myapplication;



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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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();


    }
}













All Server Links to Download This File:



Previous Post
Next Post

post written by:

0 Comments: