Text-To-Speech Application,in Android
Android Text-To-Speech Application
One of the many features that Android provides out of the box is the one of “speech synthesis”. This is also known as “Text-To-Speech” (TTS) and is mainly the capability of the device to “speak” text of different languages. This feature was introduced in version 1.6 of the Android platform and you can find an introductory article at theofficial Android-Developers blog.
In this tutorial I am going to show you how to quickly introduce TTS capabilities into your application.
Let’s get started by creating a new Eclipse project under the name “AndroidTextToSpeechProject” as shown in the following image:
Note that Android 1.6 was used as the build target and that for the minimum SDK version I used the value 4, since this functionality is not provided by the previous versions.
The first step to use the TTS API is to check if it is actually supported by the device. For that purpose there is a special action namedACTION_CHECK_TTS_DATA which is included in the TextToSpeech.Engine. As the Javadoc states, this is used by an Intent to “verify the proper installation and availability of the resource files on the system”. In case the resourses are not available, another action namedACTION_INSTALL_TTS_DATA is used in order to trigger their installation. Note that the SDK’s emulator supports TTS with no configuration needed.
Before using the TTS engine, we have to be sure that it has properly been initialized. In order to get informed on whether this has happened, we can implement an interface called OnInitListener. The method onInit will be invoked when the engine initialization has completed with the accompanying status.
After initialization, we can use the TextToSpeech class to make the device speak. The relevant method is named speak, where the text, the queue mode and some additional parameters can be passed. It is important to describe queue mode. It is the queuing strategy to be used by the TTS engine, i.e. what to do when a new text has been queued to the engine. There are two options:
- QUEUE_ADD: the new entry is added at the end of the playback queue
- QUEUE_FLUSH: all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry
All the above are translated to the code provided below:
The engine support is checked in the onActivityResult method. If there is support for TTS, we initialize the engine, else a new Intent is launched in order to trigger the relevant installation. After initialization, we used a Toast in order to notify the user about the operation status. Finally, we use a text field where the text is provided by the user and a button which feeds the engine with the provided text.
In order to run the project, an appropriate emulator device is needed. If you have none available, use the following configuration in the AVD manager (note that “Android 1.6 – API Level 4” or higher is needed):
Provide the text you wish to hear into the text field, hit the button and listen to emulator speak to you!
That’s all folks, nice and simple. Now, make your devices speak! You can download the created Eclipse project here.