Half Dozen Hello Worlds Part 4 – Android Text to Speech

This is part 4 of a multi-part series that looks at some basic Android I/O using simple Hello World programs. At this point I’ll assume you know how to make a new Android Project in Eclipse as that was covered in Part 1.

In this part we are going to look at using Android’s Text to Speech capabilities to get our Android device (or emulator) to actually say “Hello World” to the user. 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). Here is the code we are going to use to make our Android device speak to us.

This is different from what we have seen before because it has the extra part at the end “implements OnInitListener”. If you are familiar with Java you know that implements the OnInitListener interface. This will allow our activity to respond to the event triggered when the TextToSpeech engine finishes initializing. We will use the onInit method for this purpose later in our code.

Inside the class I’ve defined a class variable:

private TextToSpeech tts;

so it will be available to both methods in our activity. Then in the onCreate method I initialize the variable using the TextToSpeech class.

tts =new TextToSpeech(this, this);

If you get an error on this line make sure you have the import android.speech.tts.TextToSpeech; statement at the top. in Eclipse you can use Ctrl + Shift + O to automatically update your import statements.

The constructor for the TextToSpeech class takes two parameters. The first specifies the the context in which we’re running, and the second is the OnInitListener that should be called. Since our Hello4 class inherits from Activity we can use it for the context, and since it implements OnInitListener we can use it for the OnInitListener. Thus I simply provided the keyword “this” for both parameters.

Since we specified our class as the onInitListener to use, the class method onInit will automatically be called when the TextToSpeech class is initialized.

This method is passed a integer indicating the status after initialization. We will compare this status code to the constant provided by the TextToSpeech class to indicate successful initialization (TextToSpeech.SUCCESS). If Initialization was successful we will call the speak method to actually have your android device speak “Hello World”.

tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);

This method takes three parameters. The first is the text that should be spoken. The second is the mode to use for speech. The mode used for speech can allow you to either speak the text immediately (using QUEUE_FLUSH as we did here) or queue up multiple strings to be read. Since this is intended to be a very simple example we won’t cover this functionality here. The final parameter allows you to pass a list of additional parameters. This is also outside the scope of this example.

This is the end of our tutorial on Text To Speech. Thanks for reading and be sure to check back; there is one more Hello World example forthcoming.

{ 4 } Comments

I am newbie to this android world. So I have gone through various blogs and tutoriols but your’s is the most fundamental and explained I must say.Even i didn’t know about openGL(I hv gone through this) i got it fully.
Thank you very much.