Skip to content

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.

package com.learnandroid.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
 
public class Hello4 extends Activity implements OnInitListener
{
 
	private TextToSpeech tts;
	static final int TTS_CHECK_CODE = 0;
 
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        tts = new TextToSpeech(this, this);
    }
 
	@Override
	public void onInit(int initStatus) {
		if (initStatus == TextToSpeech.SUCCESS)
		{
			tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
		}
	}
}

The first thing you should notice here is the line:

public class Hello4 extends Activity implements OnInitListener

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.

	public void onInit(int initStatus) {
		if (initStatus == TextToSpeech.SUCCESS)
		{
			tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
		}
	}

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

  1. crazy soft | January 9, 2011 at 4:30 pm | Permalink

    Great blog, near fellow that wrote so often. The pipe thing that writes to-date information :)

  2. Uday | March 22, 2011 at 6:43 am | Permalink

    Hello Sir,

    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.

  3. Gurmeet | April 12, 2011 at 12:20 pm | Permalink

    Awesome tutorial. Thanks a lot…!! :)

  4. shailesh n vora | February 28, 2014 at 3:45 am | Permalink

    Great Tutorial
    Thanks for posting such a nice tutorial helping me a lot as i am beginner

    Again thanks

{ 2 } Trackbacks

  1. [...] * Email * Website Comment « Half Dozen Hello Worlds Part 2 – Using Layout XML Half Dozen Hello Worlds Part 4 – Android Text to Speech [...]

  2. [...] Part 4 explored using Text to Speech to have Android actually say “Hello World” out loud. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *