Android supports TextToSpeech (TTS) from version 1.6 onwards. The Android TextToSpeech can speak different languages such as English, German, French, etc…, but we have to download the language resource files first before using it. We can install the language files by launching Intent with action set to TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA. The following code launches the installation of language data:
Intent intent = new Intent(); intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(intent);
The class android.speech.tts.TextToSpeech is used for TTS in Android. The constructor has two parameters, Context and TextToSpeech.OnInitListener. The TextToSpeech.OnInitListener’s onInit method will be called when the TTS engine is initialized. The status is passed to the onInit(int status) method. The value TextToSpeech.SUCCESS indicates success and TextToSpeech.ERROR indicates error.
Once it is successfully initialized we can use the speak method to start speaking. The syntax of speak method is:
public int speak(String text, int queueMode, HashMap params)
The first parameter is the text to speak, second is parameter is the queuing mode. This parameter can be either TextToSpeech.QUEUE_ADD or TextToSpeech.QUEUE_FLUSH. The value TextToSpeech.QUEUE_FLUSH tells the system to drop all entries in the queue and start with the new text. TextToSpeech.QUEUE_ADD tells the system to append the new text the queue. The third parameter is a HashMap The key can have values TextToSpeech.Engine.KEY_PARAM_STREAM or TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID. If the key is TextToSpeech.Engine.KEY_PARAM_STREAM, then the value is one of the AudioManger’s stream types. When you specify one of these values, the TextToSpeech engine will use the settings of that particular stream to speak the text. Some of the possible values are:
String.valueOf(AudioManager.STREAM_ALARM) String.valueOf(AudioManager.STREAM_MUSIC) String.valueOf(AudioManager.STREAM_RING)
If the key is TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, then the value is a user defined string id. This can be used to check whether the TTS engine finished speaking the text or not. When the TTS engine finished speaking the text, the TextToSpeech.OnUtteranceCompletedListener’s onUtteranceCompleted method will be called. This technique is used in the sample application. For more information refer to the source code of the sample.
We can change the TTS engine’s language by using the method setLocale(Locale loc). The parameter loc specifies the Locale corresponds to desired language.
The speech rate can be set by using the method setSpeechRate(float speechRate). The parameter speechRate specifies the rate of the speech. 1 is normal rate. Lower values slow the speech rate and higher values increases the rate.
To change the pitch we use setPitch(float pitch). The parameter pitch specifies the pitch to use. 1 means normal, less than 1 lowers the tone and greater that 1 increases the tone.
I hope this gives you an introduction to the TTS in Android, for more detailed information refer to this excellent article.
Happy TTS coding!