Implementation method of text content automatic reading function developed by Android

This paper describes the implementation method of text content automatic reading function developed by Android. Share with you for your reference, as follows:

Android provides automatic reading support. Automatic reading support can read the specified text content to generate sound; Not only that, Android's automatic reading support also allows you to record the audio corresponding to the text into an audio file for later playback. The English name of this automatic reading support is texttospeech, or TTS for short.

With the support of TTS, audio output can be dynamically increased in the application, so as to improve the user experience.

Android's automatic reading support is mainly completed through texttospeech, which provides the following constructor:

It is not difficult to read from the above constructor. When creating a texttospeech object, you must first provide an oninitlistener listener, which is responsible for listening to the initialization results of texttospeech.

Once the texttospeech object is obtained in the program, you can call the setlanguage (localeloc) method of texttospeech to set the language and country options that the TTS sound engine should use.

If the return value of calling setlanguage (localeloc) is "texttospeech. Lang_country_available", it indicates that the current TTS system can support the set language and country options.

After setting texttospeech, you can call its method to read the text. For specific methods, please refer to the API document of texttospeech. The most common methods in the texttospeech class are the following two.

The above two methods are used to convert text content into audio. The only difference is that the speak method plays the converted audio, while the synthesizetofile saves the converted audio as a sound file.

The params in the above two methods are used to specify the parameters during sound conversion. The queuemode parameter in the speak () method specifies the TTS pronunciation queue mode. This parameter supports the following two constants.

TextToSpeech.QUEUE_ Flush: if this mode is specified, when TTS calls the speak method, it will interrupt the running task of the current instance (it can also be understood as knowing the current voice task and executing a new voice task instead)

TextToSpeech.QUEUE_ Add: if this mode is specified, when TTS calls the speak method, a new pronunciation task will be added to the current pronunciation task queue - that is, the pronunciation task specified by the speak () method will be executed after the pronunciation task in the task queue is completed.

After the program has run out of the TextToSpeech object, it can call its shutdown () in the OnDestroy () method of Activity to close TextToSpeech and release the resources it occupies.

To sum up, the steps of using texttospeech are as follows

1. Create a texttospeech object. When creating, pass in oninitlistener listener to listen whether the creation is successful.

2. Set the language and country options used by texttospeech, and judge whether TTS supports the language and country options through the return value.

3. Call the speak () or synthesizetofile method.

4. Close TTS and recycle resources.

The following program demonstrates how to use TTS to read the text content entered by the user.

Speech.java:

In the above program, the first line of bold code setting creates a texttospeech object, and the second line of bold is read in American English. Next, the program has two buttons respectively. One button is used to execute reading and the other button is used to save the text content reading audio as a sound file, which is completed by calling the two methods of texttospeech object.

Run the program, and you can see the following interface:

In the interface, when the user clicks the "read aloud" button, the system will call the TTS speak() method to read the contents of the text box; When the user clicks the "record sound" button, the system will call the synthesizetofile() method to record the reading audio corresponding to the text in the text box into the sound file of the SD card - after clicking this button, a sound.wav file can be generated in the root directory of the SD card, which can be exported and played in other audio playback software.

The program rewrites the ondestroy () method of the activity, closes the texttospeech object in this method, and reclaims its resources.

Tip: because different characters have different pronunciation in different languages and countries, especially in Europe and the United States, they use alphabetic characters. Therefore, the pronunciation effect of a paragraph of text content is quite different when it is read aloud in different languages and countries. At present, Android TTS does not support Chinese temporarily.

For more Android related content, readers who are interested can view the special topics of this site: summary of Android multimedia operation skills (audio, video, recording, etc.), introduction and advanced tutorial of Android development, @ l_404_2@, summary of activity operation skills of Android programming, summary of Android database operation skills, summary of Android file operation skills Summary of Android resource operation skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>