What is TTS?
TTS, short for Text To Speech, is the part of the human-computer conversation that allows machines To speak.
TTS technology can convert text files in real time, and the conversion time can be calculated in seconds.
TTS not only helps visually impaired people read information on computers, but also increases the readability of text documents. Today’s TTS applications include voice-driven mail and voice sensitive systems, and are often used in conjunction with voice recognition programs.
Developer official website introduction
TextToSpeech must be instantiated before it can be used.
Implement TextToSpeech. OnInitListener method to get the instantiation results listening in. When you have finished using the TextToSpeech instance, you should call the shutdown() method to release the local resources used by TextToSpeech.
A constructor
/ / use the default engine TextToSpeech (Context Context, TextToSpeech. OnInitListener listener) / / using the specified engine TextToSpeech (Context Context, TextToSpeech.OnInitListener listener, String engine)Copy the code
The main method
/** * text text that needs to be converted to voice * queueMode queueMode: * QUEUE_ADD: this content is broadcast only after the previous voice task has been played * QUEUE_FLUSH: * params Sets the TTS parameter, which can be null. * KEY_PARAM_STREAM: specifies the audio channel, including STREAM_MUSIC, STREAM_NOTIFICATION, STREAM_RING, etc. * KEY_PARAM_VOLUME: specifies the volume, 0-1f * utteranceId: Id */ textToSpeech. Speak (Content, textToSpeech. QUEUE_FLUSH, NULL, I +""); Texttospeech.stop (); // Close and release the resource textTospeech.shutdown (); Texttospeech.setpitch (0.5f); textToSpeech setPitch(0.5f); textToSpeech setPitch(0.5f); / / set the speed, the default 1.0 speed textToSpeech. SetSpeechRate (1.5 f);Copy the code
Used in the project
Create the TextToSpeech class
// Using the default engine, pass in Context and OnInitListener TextToSpeech toSpeech = new TextToSpeech(this, this);Copy the code
2. Override onInit()
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = toSpeech.setLanguage(Locale.CHINA);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Data lost or unsupported", Toast.LENGTH_SHORT).show();
}
if(toSpeech ! = null) {toSpeech. SetPitch (1.0 f); // Set the tone, the higher the value, the higher the sound (girls), The smaller the list_task_little.get(currentPosition).gettask_content (), TextToSpeech.QUEUE_FLUSH, null; }}}Copy the code
Call method int result = tospeech.setLanguage (locale.China); Set the language to Chinese
In the onInit() method to determine whether the initialization is successful, use tospeech.setPitch (1.0f) to set the pitch, the larger the value, the higher the pitch.
Using textToSpeech. SetSpeechRate set speed (1.5 f), the default 1.0 normal speed.
3. Free up resources
@Override
protected void onStop() { super.onStop(); toSpeech.stop(); // TTS interrupted whether or not you are reading tospeech. shutdown(); // Close, release resources}Copy the code
Release resources when a page needs to exit or is no longer using TTS.