Download source code of this article.
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, HashMapparams)
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
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!
Dear KRISHNARAJ VARMA,
I’m an Android developer from Vietnam. So, I have a duty of TTS that wish to work with Vietnamese language. Are there any solutions that allow us to add new language package to TTS engine? I mean I want to use TTS with a new language that not to be listed from the original engine. Ex: setLocale(“vi”), smt like this and the language resource files was customized!
Hope to your reply! Thank you!