Text To Speech in android

Android allows you convert your text into voice. Not only you can convert it but it also allows you to speak text in variety of different languages.

Android provides TextToSpeech class for this purpose. In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below −

private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
   @Override
   public void onInit(int status) {
   }
});

In this listener, you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below −

ttobj.setLanguage(Locale.UK);

The method setLanguage takes an Locale object as parameter. The list of some of the locales available are given below −

Sr.No Locale
1 US
2 CANADA_FRENCH
3 GERMANY
4 ITALY
5 JAPAN
6 CHINA

Once you have set the language, you can call speak method of the class to speak the text. Its syntax is given below −

ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

Apart from the speak method, there are some other methods available in the TextToSpeech class. They are listed below −

Sr.No Method & description
1

addSpeech(String text, String filename)

This method adds a mapping between a string of text and a sound file.

2

getLanguage()

This method returns a Locale instance describing the language.

3

isSpeaking()

This method checks whether the TextToSpeech engine is busy speaking.

4

setPitch(float pitch)

This method sets the speech pitch for the TextToSpeech engine.

5

setSpeechRate(float speechRate)

This method sets the speech rate.

6

shutdown()

This method releases the resources used by the TextToSpeech engine.

7

stop()

This method stop the speak.