Creating a Text to Speech Application

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 information and click Next. 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 graphicaly 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
This entry was posted in Uncategorized. Bookmark the permalink.