Android Text To Speech - Androhub

Text to Speech

Android Text To Speech

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 can implement TextToSpeech.OnInitListener this listener to your class and it will automatically add the below method to your class,

else you can specify the initListener, its syntax is given below:

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 −

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

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

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

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

MethodDescription
addSpeech(String text, String filename)This method adds a mapping between a string of text and a sound file.
getLanguage()This method returns a Locale instance describing the language.
isSpeaking()This method checks whether the TextToSpeech engine is busy speaking.
setPitch(float pitch)This method sets the speech pitch for the TextToSpeech engine.
setSpeechRate(float speechRate)This method sets the speech rate.
shutdown()This method releases the resources used by the TextToSpeech engine.
stop()This method stop the speak.

If you want to do the other way i.e converting speech to text, follow my tutorial Android Speech to Text.

Example

In this tutorial, we are going to learn how to convert Text to Speech.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. First create a layout in xml file naming activity_main.xml that contains Button for starting TTS(TextToSpeech) and an EditText that take input and convert the text into speech.

3. Finally come to your MainActivtiy.java and add the following code. This class contains all the code that we discussed earlier in this article.

4. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *