Android Text To Speech Voice Example tutorial with source code download

How to create an android app which will get EditText value and convert text into audio speech programmatically.

Android mobile operating system covers more then 40% market of mobile devices and increasing day by day. There are millions of android apps available on Google play store so in this tutorial we are going to do something new and design a simple android app which known as Android Text To Speech Voice Example tutorial with text conversion to direct speech . With this android application android developers can gives the facility to its user so they can convert EditText typed text values to speech . So here is the complete step by step tutorial for Android Text To Speech Voice Example tutorial with source code download .

android-project-download-code-button

Android Text To Speech Voice Example tutorial with source code download.

Code for MainActivity.java file.

package com.android_examples.text2speech_android_examplescom;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;

public class MainActivity extends AppCompatActivity
        implements TextToSpeech.OnInitListener {

    TextToSpeech textToSpeech;
    Button button;
    EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textToSpeech = new TextToSpeech(MainActivity.this, MainActivity.this);

        button = (Button) findViewById(R.id.button);

        editText = (EditText) findViewById(R.id.editText);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                TextToSpeechFunction() ;
            }

        });
    }

    public void TextToSpeechFunction()
    {

        String textholder = editText.getText().toString();

        textToSpeech.speak(textholder, TextToSpeech.QUEUE_FLUSH, null);

        Toast.makeText(MainActivity.this , textholder, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {

        textToSpeech.shutdown();

        super.onDestroy();
    }

    @Override
    public void onInit(int Text2SpeechCurrentStatus) {

        if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {

            textToSpeech.setLanguage(Locale.US);

                button.setEnabled(true);

                TextToSpeechFunction();
            }

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.text2speech_android_examplescom.MainActivity">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="Enter Text Here"
        android:gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click here to convert EditText text into speech"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp" />
</RelativeLayout>

Android Text To Speech Voice Example tutorial with source code download

 

 Click here to download Android Text To Speech Voice Example tutorial with source code download.

Leave a Reply

Your email address will not be published. Required fields are marked *