Code for Text to Speech Conversion in Android

Code for text to speech conversion in Android.

Steps:

  • Start Eclipse IDE.
  • Create a new project.
  • Create a MainActivity.java file.
  • Create an activity_main.xml for layout design.

MainActivity.java

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.speech.tts.TextToSpeech;

import android.speech.tts.TextToSpeech.OnInitListener;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity implements OnInitListener
{

    EditText e1;

    Button b1;

    TextToSpeech tts;

 

    @Override

    protected void onCreate(Bundle savedInstanceState)
    {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       e1=(EditText)findViewById(R.id.editText1);

       b1=(Button)findViewById(R.id.button1);

       b1.setOnClickListener(new OnClickListener()
       {

           @Override

           public void onClick(View v)
           {

               // TODO Auto-generated method stub

               String str = e1.getText().toString();

               tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);

            }

        });

        Intent i=new Intent();

        i.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

        startActivityForResult(i, 1);

 

    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

         //TODO Auto-generated method stub

         super.onActivityResult(requestCode, resultCode, data);

         if(requestCode==1)

         {

             if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)

             {

                tts=new TextToSpeech(this,this);

             }

             else

             {

                Intent i = new Intent();

                i.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

                startActivity(i);

             }

         }

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu)
    {

       // Inflate the menu; this adds items to the action bar if it is present.

       getMenuInflater().inflate(R.menu.main, menu);

       return true;

    }

    @Override

    public void onInit(int status)
    {

        if(status==TextToSpeech.SUCCESS)

        {

           // TODO Auto-generated method stub

           Toast.makeText(getApplicationContext(),"engine installed", 1000).show();

        }

        if(status==TextToSpeech.ERROR)

        {

            Toast.makeText(getApplicationContext(),"engine not installed", 1000).show();

        }

    }

Code - activity_main.xml

<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=".MainActivity" >

  <TextView

  android:id="@+id/textView1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="@string/hello_world" />

  <EditText

  android:id="@+id/editText1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignLeft="@+id/textView1"

  android:layout_below="@+id/textView1"

  android:layout_marginLeft="35dp"

  android:layout_marginTop="32dp"

  android:ems="10" >

    <requestFocus />

  </EditText>

  <Button

  android:id="@+id/button1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignLeft="@+id/editText1"

  android:layout_below="@+id/editText1"

  android:layout_marginLeft="16dp"

  android:layout_marginTop="62dp"

  android:text="Button" />

</RelativeLayout>

 

 Output