Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of automatic reading of text content in Android

2025-03-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the automatic reading function of text content in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the automatic reading function of text content in Android".

Android provides automatic reading support. Automatic reading support allows you to read specified text content aloud, resulting in sound; not only that, Android's automatic reading support also allows the audio corresponding to the text to be recorded 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, thus improving the user experience.

Android's auto-reading support is mainly done through TextTospeech, which provides the following constructor:

TextTospeech (Context context, TextTospeech.OnInitListenerlistener)

It is not difficult to read from the constructor above, and when creating a TextTospeech object, you must first provide an OnInitListener listener-- which listens to the initialization results of the TextTospeech.

Once you have obtained the TextTospeech object in the program, you can then 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", the current TTS system can support the set language and country options.

Once you have set up TextToSpeech, you can call its method to read the text aloud, which can be found in TextToSpeech's API documentation. The most commonly used methods in the TextToSpeech class are the following two.

Speak (Stringtext, int queueMode,HashMapparams) synthesizeToFile (Stringtext,HashMapparams,String filename)

The above two methods are used to convert text text content into audio, except that the speak method plays the converted audio, while synthesizeToFile saves the converted audio into a sound file.

The params in the above two methods is used to specify the parameters for sound conversion, and the queueMode parameter in the speak () method specifies the TTS pronunciation queue mode, which supports the following two constants.

TextToSpeech.QUEUE_FLUSH: if this mode is specified, when TTS calls the speak method, it interrupts the running task of the current instance (it can also be understood as knowing the current voice task and performing the new voice task)

TextToSpeech.QUEUE_ADD: if this mode is specified, when TTS calls the speak method, the new pronunciation task is added to the current pronunciation task queue-that is, the pronunciation task specified by the speak () method is executed after the completion of the task queue.

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

To sum up, the steps to use TextToSpeech are as follows

1. Create a TextToSpeech object, and pass the OnInitListener listener to check whether the creation is successful.

2. Set the language and country options used by TextToSpeech, and determine whether TTS supports the language and country options by 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 entered by the user.

Speech.java:

Public class Speech extends Activity {TextToSpeech tts; EditText editText; Button speech; Button record; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main) / / initialize the TextToSpeech object tts = new TextToSpeech (this, new OnInitListener () {@ Override public void onInit (int status) {/ / if the TTS engine is loaded successfully if (status = = TextToSpeech.SUCCESS) {/ / set to read int result = tts.setLanguage (Locale.US) in American English / / if the set language if (result! = TextToSpeech.LANG_COUNTRY_AVAILABLE & & result! = TextToSpeech.LANG_AVAILABLE) {Toast.makeText (Speech.this, "TTS does not support reading aloud in this language for the time being." , 50000) .show ();}}); editText = (EditText) findViewById (R.id.txt); speech = (Button) findViewById (R.id.speech); record = (Button) findViewById (R.id.record) Speech.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {/ / perform tts.speak (editText.getText () .toString (), TextToSpeech.QUEUE_ADD, null);}}) Record.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {/ / record the audio of reading text aloud to the specified file tts.synthesizeToFile (editText.getText (). ToString (), null, "/ mnt/sdcard/sound.wav"); Toast.makeText (Speech.this, "Sound recording successful!" , 50000) .show ();}}) @ Override public void onDestroy () {/ / close the TextToSpeech object if (tts! = null) {tts.shutdown ();}

The first line of bold code setting in the above program creates a TextToSpeech object, and the second line of bold words is read aloud in American English. Next, the program systematizes two buttons, one button is used to perform the occurrence of reading aloud, and the other button is used to save the audio of the text content as a sound file, which is accomplished by calling two methods of the TextToSpeech object.

When you run the program, you can see the following interface:

In the interface, when the user clicks the "read" button, the system will call the speak () method of TTS 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 overrides the onDestroy () method of Activity, where it closes the TextToSpeech object and reclaims its resources.

Thank you for your reading, the above is the content of "how to realize the automatic reading function of text content in Android". After the study of this article, I believe you have a deeper understanding of how to achieve the automatic reading function of text content in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report