Android tutorial - Android Text To Speech - android app development - android studio - android development tutorial
Learn android - android tutorial - Android text to speech - android examples - android programs
Definition of TextToSpeech:
- Text to speech, abbreviated as TTS, is a form of speech synthesis that converts text into spoken voice output.
- Text to speech systems were first developed to aid the visually impaired by offering a computer-generated spoken voice that would "read" text to the user.
- In android, you can convert your text into speech by the help of TextToSpeech class.
- After completion of the conversion, you can playback or create the sound file.
Constructor of TextToSpeech class
- TextToSpeech(Context context, TextToSpeech.OnInitListener)
Methods of TextToSpeech class
- The commonly used methods of TextToSpeech class are as follows:
Method | Description |
---|---|
int speak (String text, int queueMode, HashMap params) | converts the text into speech. Queue Mode may be QUEUE_ADD or QUEUE_FLUSH. Request parameters can be null, KEY_PARAM_STREAM, KEY_PARAM_VALUME etc. |
int setSpeechRate(float speed) | it sets the speed for the speech. |
int setPitch(float speed) | it sets the pitch for the speech. |
int setLanguage (Locale loc) | it sets the locale specific language for the speech. |
void shutdown() | it releases the resource set by TextToSpeech Engine. |
int stop() | it interrupts the current utterance (whether played or rendered to file) and discards other utterances in the queue. |
TextToSpeech.OnInitListener Interface
- You need to implement TextToSpeech.OnInitListener interface, for performing event handling on TextToSpeech engine.
Method of TextToSpeech.OnInitListener Interface
- There is only one method in this interface.
Method | Description |
---|---|
void onInit (int status) | Called to signal the completion of the TextToSpeech engine initialization. The status can be SUCCESS or ERROR. |
Android TextToSpeech Example
- Let's write the code to convert text into voice.
activity_main.xml
- Drag one textview, one edittext and one button for the layout. Now the activity_main.xml file will look like this:
File: activity_main.xml
Activity class
- Let's see the code to speak the given text.