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: WikiFormat

Problem:

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

Solution:

Use the TextToSpeech api.

Discussion:

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.setLanguage(Locale.US);
myTTS.speak(textToBeSpoken, TextToSpeech.QUEUE_FLUSH, null);
myTTS.shutdown();

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')");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button startButton = (Button) findViewById(R.id.start_button);
		startButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent checkIntent = new Intent();
				checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
				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
				myTTS.setLanguage(Locale.US);
			} else {
				// TTS data not yet loaded, try to install it
				Intent ttsLoadIntent = new Intent();
				ttsLoadIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(ttsLoadIntent);
			}
		}
	}

	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) {
			myTTS.shutdown();
		}
	}

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.

Download:

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

Download:

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