Creating a Text to Speech Application

Creating a Text to Speech Application is surprisingly simple on android. I’m going to write a quick post with some great pointers on how to accomplish this.

What you need:
Android SDK
Eclipse (I’m using Helios)

Lets begin:

First we need to create a new android project in Eclipse. In Eclipse Click File, New, and select android project. You will be created with the following screen. Enter your project name and click Next to continue.

Next we need to select a target platform. I have chosen 2.2, make your selection and click Next to continue.

Now we need to name our package name, activity name, and set the minimum SDK. Enter your information and select Finish to complete creating the android project.

Now that we have the project created lets get started with the good stuff. Start off by creating the layout. open /res/layout/main.xml

This is the screen for the application, it is also by default created by eclipse.

In this document we are going to create an EditText for user input and a button to start the text to speech.

Lets add that EditText:
delete the text view that eclipse adds by default.
Add the EditText in between the LinearLayout

<EditText
android:id=”@+id/Speech”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:hint=”Enter Text for Speech!”
/>

Alright now that we have the Edit Text added we can continue on to adding the button.

Now add the following into your xml

    <Button
android:id=”@+id/Speak”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Speak!”
android:onClick=”onClick”/>

The complete main.xml should look like the following

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<EditText
android:id=”@+id/Speech”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:hint=”Enter Text for Speech!”
/>

<Button
android:id=”@+id/Speak”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Speak!”
android:onClick=”onClick”/>

</LinearLayout>

Lets take a look graphically at what we have created.

Open your java file under /src/’your package name’/
Once opened you can see where eclipse has done some of the work for you. You content view has already been set to your main.xml

Start off by adding the EditTex,Speech button, and The Text To Speech into the class.

With the code below you have noticed that I have added ‘implements OnInitListener, OnUtteranceCompletedListener” You must add this as TextToSpeech uses this to check to see if the required data for TextToSpeech is installed.

public class TextToSpeechExampleActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
/** Called when the activity is first created. */

Button Speak;
EditText Speech;
private TextToSpeech mTts;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Speak = (Button)this.findViewById(R.id.Speak);
Speech = (EditText)this.findViewById(R.id.Speech);
//initialize TTS
mTts= new TextToSpeech(this, this);

}

public void onInit(int initStatus) {
mTts.setOnUtteranceCompletedListener(this);

}
public void onUtteranceCompleted(String utteranceId) {

mTts.shutdown();

}

You will notice that you will have some errors once this code has been written. The error should be the following

“Button cannot be resolved as a type”

“EditText cannot be resolved as a type”

“TextToSpeech cannot be resolved as a type”

The fix for these errors are very simple. Simply go over each error and have eclipse import Button, EditText, and TextToSpeech. There is a even easier way to do this with eclipse. Press Ctrl+Shift+O. This will organize your imports for you and even delete unused ones. Once you have imported Button, EditText, and TextToSpeech we should be error free.

So Lets keep trucking, the only thing we lack is what the button is finally going to do!

I handle my button clicks using the onClick

insert the code below into your class

public void onClick(View v)

{

switch(v.getId())

{

case R.id.Speech:

//when Button Speak is clicked

//grab text from the EditText Speech
String TALK = Speech.getText().toString();

//Talk now has text from EditText

mTts.speak(TALK, TextToSpeech.QUEUE_ADD, null);
break;

}

}

The complete class should look like the following

“Your package name goes here”;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TextToSpeechExampleActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
/** Called when the activity is first created. */

Button Speak;
EditText Speech;
private TextToSpeech mTts;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Speak = (Button)this.findViewById(R.id.Speak);
Speech = (EditText)this.findViewById(R.id.Speech);
//initialize TTS
mTts= new TextToSpeech(this, this);

}

public void onInit(int initStatus) {
mTts.setOnUtteranceCompletedListener(this);

}
public void onUtteranceCompleted(String utteranceId) {

mTts.shutdown();

}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.Speak:
//when Button Speak is clicked

//grab text from the EditText Speech
String TALK = Speech.getText().toString(); //Talk now has text from EditText

mTts.speak(TALK, TextToSpeech.QUEUE_ADD, null);

break;
}
}
}

There you go! That ties up the Text to Speech Application if you have any questions about the posted code or have any suggestions. Feel free to contact us or leave a comment. Thanks

About Jordan

Hello, My name is Jordan Sims. I'm a software developer for The Better Source. I have 1 year of android development and I am current going to school for High Performance Computing.
This entry was posted in Software Dev - General. Bookmark the permalink.

14 Responses to Creating a Text to Speech Application

  1. Maynard says:

    greetings, great wordpress blog, and an amazing understand! definitely one for my favorites.

  2. I hope other people find your blog post listed here as helpful as I have. I run a weblog myself and would be pleased for you or the readers on your own website to check out. Please go ahead and read through my site just like I have with your own and leave a remark or two if you discover anything interesting. Thank you.

  3. Jean says:

    Gee whiz, and I toughht this would be hard to find out.

  4. Shermaine says:

    There’s a sercet about your post. ICTYBTIHTKY

  5. Dotty says:

    You’re the one with the barins here. I’m watching for your posts.

  6. Brandilyn says:

    How could any of this be better settad? It couldn’t.

  7. Jeneva says:

    Big help, big help. And superlative news of curose.

  8. Digger says:

    I was seirously at DefCon 5 until I saw this post.

  9. Belle says:

    All of my quseitons settled-thanks!

  10. Micheal says:

    Full of salient points. Don’t stop believing or wrtniig!

  11. Strongly suggest adding a “google+” button for the blog!

Leave a Reply

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