home top ad

How to create Text to Speech app in android studio

Today we are learn about how to create to text to speech in android studio.Meaning when we input any text in edittext and submit it then it will read that english word.
Its code is given below.
 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:transitionGroup="true"
    android:padding="16dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:text="Tap to speak up"
        android:textColor="@color/black"
        android:textSize="28sp" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:hint="Enter A text.." />

    <Button
        android:id="@+id/tap_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Tap to speak" />

    <TextView
        android:id="@+id/taptext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tap on the mic"
        android:gravity="center"
        android:textStyle="bold"
        android:layout_marginTop="30dp"
        android:textSize="30dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tap_button"
        />

    <ImageView
        android:id="@+id/mic"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_below="@id/taptext"
        android:src="@drawable/ic_mic"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"/>

</RelativeLayout>
 MainActivity.java
package com.testproject.testproject;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    private EditText editText;
    Button btn;
    ImageView mic;
    TextToSpeech textToSpeech;
    String str;
    private final  int REQUEST_CODE=100;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.edit);
        btn=findViewById(R.id.tap_button);
        mic=findViewById(R.id.mic);
        textToSpeech=new TextToSpeech(this,(TextToSpeech.OnInitListener)this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speakUp();
            }
        });
        mic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say something");
                try {
                    startActivityForResult(intent,REQUEST_CODE);
                }
                catch(Exception e)
                {
                    Toast.makeText(SpeakeActivity.this, "Sorry! device does not support speech input", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void speakUp() {
        String text=editText.getText().toString();
        textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQUEST_CODE:{
                if (resultCode==RESULT_OK && null!=data){
                    ArrayList<String>res=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    editText.setText(res.get(0));
                    Toast.makeText(SpeakeActivity.this, res.get(0), Toast.LENGTH_SHORT).show();
                    textToSpeech.speak(res.get(0),TextToSpeech.QUEUE_FLUSH,null);
                }
                break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        if(textToSpeech!=null){
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onInit(int i) {
        if (i== TextToSpeech.SUCCESS){
            int result=textToSpeech.setLanguage(Locale.US);
            if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e("TTS","This Language not supported");
            }
            else
            {
                btn.setEnabled(true);
                speakUp();
            }
        }
        else
        {
            Log.e("TTS","Innitilization Failed");
        }
    }
}
Below is the screenshot of this app how this app will work.
Thanks you

Post a Comment

0 Comments