Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Making the Device Speak with Text-to-Speech

Author: Ian Darwin -- Published? true -- FormatLanguage: W


You want your application to pronounce words of text so the user can perceive them without watching the screens (e.g., when driving).


Use the TextToSpeech api.


The TextToSpeech API is built in to Android (though you may have to install the voice files, depending on version).

To get started you just need a TextToSpeech object. In theory you could just do this:

private TextToSpeech myTTS = new TextToSpeech(this, this);
myTTS.speak(textToBeSpoken, TextToSpeech.QUEUE_FLUSH, null);

However, to ensure success, you actually have to use a couple of intents, one to check that the TTS data are available and/or install them if not, and another to start the TTS mechanism. So in practice the code needs to look something like the following. This quaint little application chooses one of half a dozen banal phrases to utter each time the Speak button is pressed.

public class Main extends Activity implements OnInitListener {

	private TextToSpeech myTTS;
	private List<String> phrases = new ArrayList<String>();

	public void onCreate(Bundle savedInstanceState) {

		phrases.add("Hello Android, Goodbye iPhone");
		phrases.add("The quick brown fox jumped over the lazy dog");
		phrases.add("What is your mother's maiden name?");
		phrases.add("Etaoin Shrdlu for Prime Minister");
		phrases.add("The letter 'Q' does not appear in 'antidisestablishmentarianism')");

		Button startButton = (Button) findViewById(R.id.start_button);
		startButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent checkIntent = new Intent();
				startActivityForResult(checkIntent, 1);	

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {

			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				myTTS = new TextToSpeech(this, this); // 1
			} else {
				// TTS data not yet loaded, try to install it
				Intent ttsLoadIntent = new Intent();

	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int n = (int)(Math.random() * phrases.size());
			myTTS.speak(phrases.get(n), TextToSpeech.QUEUE_FLUSH, null);

		} else if (status == TextToSpeech.ERROR) {

At the line marked "1", midway through the above code, the first argument is a Context (the Activity) and the second is an OnInitListener, also implemented by the Main Activity in this case. When the initialization of the TextToSpeech object is done, it calls the listener, whose onInit() method is meant to notify that the TTS is ready. In our trivial Speaker program here, we simply do the speaking. In a longer example you would probably want to start a thread or service to do the speaking operation.


The source code for this project can be downloaded from http://projects.darwinsys.com/Speaker..zip.


The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory Speaker.
No records found.