Media Text-to-Speech
- Introduction
- TTS for various languages and accents.
- Comes standard, but might not have some specific language resource
file.
There's a standard interface for recognizing this and retrieving
necessary files.
- See the Android
TTS page.
- Making an Android Talk
- Create a TextToSpeech object.
- Implement the TextToSpeech.OnInitListener interface,
which contains the OnInit method.
The method will be called when initialization is complete.
- Use the speak method to say things.
The second parameter can be TextToSpeech.QUEUE_FLUSH to
delete anything else in the speech queue, or
TextToSpeech.QUEUE_ADD to add to the end of the queue.
The third parameter can be null for simple speaking.
- Speech is generated and output in a new thread.
- To get a callback when each utterance is completed ...
- Make an UtteranceProgressListener object, and set
after TTS is initialised.
- In the call to speak put a unique string as the fourth
parameter.
- When the utterance is completed onDone is called with
that string as the parameter.
- Beware, onDone is running in a different thread, so
it can't do anything in the UI.
Use a callback or a Runnable to get things done in
the UI thread.
- In onDestroy release the TTS resources with
shutdown.
- Example
MainActivity.java
activity_main.xml
strings.xml