Saturday, September 8, 2012

How to use Android’s Text to Speech capabilities

This is a sample activity which shows How to use Android’s Text to Speech capabilities. Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4).
Last topic published on this forum was How to send an Email.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project MyTextToSpeech.
2.) Put the following code snippet in res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

<EditText android:id="@+id/input_text" 
<Button android:id="@+id/speak_button" 
        android:text="Speak to me"
3.) You just need to import the following packages and create a TextToSpeech object.
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
4.) The second is an OnInitListener which you will need to implement.
5.) Run the Application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyTextToSpeech. Enter following information:
Project name: MyTextToSpeech
Build Target: Android APIs 2.1
Application name: MyTextToSpeech
Package name:
Create Activity: MyTextToSpeech
On Clicking Finish MyTextToSpeech code structure is generated with the necessary Android Packages being imported along with MyTextToSpeech class will look like following:
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyTextToSpeech extends Activity implements OnInitListener{
        /** Called when the activity is first created. */
        private int MY_DATA_CHECK_CODE = 0;
        private TextToSpeech tts;
        private EditText inputText;
        private Button speakButton;
        public void onCreate(Bundle savedInstanceState) {
            inputText = (EditText) findViewById(;
            speakButton = (Button) findViewById(;
            speakButton.setOnClickListener(new OnClickListener() {                   
                public void onClick(View v) {
                   String text = inputText.getText().toString();
                   if (text!=null && text.length()>0) {
                        Toast.makeText(MyTextToSpeech.this"Saying: " + text, Toast.LENGTH_LONG).show();
                        tts.speak(text, TextToSpeech.QUEUE_ADDnull);
            Intent checkIntent = new Intent();
            startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
                if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                        // success, create the TTS instance
                        tts = new TextToSpeech(thisthis);
                else {
                        // missing data, install it
                        Intent installIntent = new Intent();
        public void onInit(int status) {             
          if (status == TextToSpeech.SUCCESS) {
                Toast.makeText(MyTextToSpeech.this"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
          else if (status == TextToSpeech.ERROR) {
                Toast.makeText(MyTextToSpeech.this"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
Output –The final output:

No comments:

Post a Comment