Monday 30 January 2012

Android Text To Speech (TTS) Basics

When I originally attempted implementing TTS functionality in my apps I was yet again surprised by how few practical tutorials there were online. I wrote a detailed, practical one a while back for Mobiletuts+ which runs through a simple app with the Text To Speech engine functions: Using the Text to Speech Engine

However, if you just want to know the basics so that you can get stuck into adding the functionality to create your own Text To Speech apps, here's what you need to do:

Import the TTS Classes
To make use of the Text To Speech API, you need to reference it in your code. Add these import statements to your chosen class for standard functionality - includes the TTS resources and others necessary for basic tasks:

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;

Implement the OnInitListener Interface
When using the TTS functions you configure your speech functionality in the onInit method, so you need your class to implement this interface. Add "implements OnInitListener" to the class declaration in which you plan on providing TTS. The following sample code demonstrates:

public class MyLovelyClass extends Activity implements OnInitListener

Check for User Data
Before you start making your apps speak you need to check that the user has the required resources on their device. Add the following instance variable at the top of your class declaration:

private int DATA_CHECKING = 0;

Next add the data checking code to the section of your class that executes when it starts up. I.E. for a standard Java class put it in the constructor method, for an Activity class put it in "onCreate" and so on:

//create an Intent
Intent checkData = new Intent();
//set it up to check for tts data
//start it so that it returns the result
startActivityForResult(checkData, DATA_CHECKING);

Create a TTS Object
So that you can access the Text To Speech object throughout the class, declare it as an instance variable at the top:

private TextToSpeech niceTTS;

When the app receives the result of your data checking operation, the onActivityResult method will execute, so add the code to instantiate the TTS in there:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 //do they have the data
 if (requestCode == DATA_CHECKING) {
 //yep - go ahead and instantiate
 if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
  niceTTS = new TextToSpeech(this, this);
 //no data, prompt to install it
 else {
  Intent promptInstall = new Intent();

Most users do have the TTS data now but this code takes care of those cases where they don't.

Provide the onInit Method
Your class is extending the OnInitListener interface so that you can set the TTS object up to function when it initialises. Add the method as follows:

public void onInit(int initStatus) {
 if (initStatus == TextToSpeech.SUCCESS) {

This is the basic process for choosing UK English but the engine offers a wide range of possibilities.

Say It Loud
Now all you need to do is speak. Add this code wherever you want the speech to occur, passing a text string to speak:

String myWords = "I love you";
niceTTS.speak(myWords, TextToSpeech.QUEUE_FLUSH, null);

This instructs the app to speak straight away, but you can optionally choose to add it to a queue, waiting for any existing speech to finish. Again, there are lots of options to explore.

These are the basics of creating a TTS app on Android, but as mentioned above there are plenty of areas in which you can tailor and adapt the functionality.

If you're having trouble getting this to work in your apps, have a go at my other tutorial first, then try doing it with your own apps again: Using the Text to Speech Engine

Other resources:
Android Developer Guide: Using Text-to-Speech
Android Developer Reference: public class TextToSpeech

No comments:

Post a Comment