SoFunction
Updated on 2025-04-07

Analysis of the usage of automatic reading TTS in Android development

This article describes the usage of Android automatic reading of TTS. Share it for your reference, as follows:

TextToSpeech, referred to as TTS, is a new and important feature since Android version 1.6. Converts the specified text to audio output in different languages. It can be easily embedded in games or applications to enhance the user experience.

Before explaining the TTS API and how to apply this feature to your actual project, have a preliminary understanding of this TTS engine.

A general understanding of TTS resources:

TTS engine relies on the five major languages ​​currently supported by Android Platform: English, French, German, Italian and Spanish (there is no great Chinese for the time being, at least Google scientists have not yet mastered Chinese, so it is natural that it is easy to start and then difficult to do so.) TTS can convert text into voice output in any of the above five languages. At the same time, for individual language versions, it will depend on different time zones. For example: for English, two different versions, American and British, can be output in TTS respectively (it shows that Google's style of doing things is really meticulous, and it is precisely because of this that another reason why Google does not add Chinese is that there are too many Chinese dialects).

To support such a huge amount of data, the TTS engine adopts a preload method for resource optimization. According to a series of parameter information (the usage of parameters will be described in detail later), the corresponding resources are extracted from the library and loaded into the current system.

Although most devices loaded with Android operating systems currently provide TTS functions through this engine, due to the very limited storage space of some devices, TTS cannot perform its functions to the maximum extent, which is a current bottleneck. To this end, the development has introduced a detection module so that applications or games that use this technology can have corresponding optimization and adjustments for different devices, thereby avoiding the use of the entire application due to the limitations of this function. A safer approach is to let users choose whether there is enough space or demand to load this resource. Here is a standard detection method:

Intent checkIntent = new Intent();
(.ACTION_C HECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

If the current system allows to create a "" rainbow simplified spectral font library download Object, it means that the TTS function has been supported, and the "CHECK_VOICE_DATA_PASS" mark is given in the detection return result. If the system does not support this function, the user can choose whether to load this function, so that the device supports the voice function "Multi-lingual Talking" that outputs multiple languages. The "ACTION_INSTALL_TTS_DATA" intent introduces the user to the TTS download interface in the Android market. The installation will be automatically completed after the download is completed. The following is the complete code to implement this process (androidres):

private TextToSpeech mTts;
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == .CHECK_VOICE_DATA_PASS) {
// sess, create the TTS instance
mTts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
(
.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}

Both the TextToSpeech entity and the OnInitListener need to reference the Context of the current Activity as a constructor parameter. The use of OnInitListener() is to notify the system that the TTS Engine has been loaded and is available.

Set language parameters according to requirements:

As early as the Google I/O conference, the official gave a fresh experience of applying this function, and directly output the translation results through voice in five different languages. The method to load the language is very simple:

();

The above code indicates that the current TTS entity is loaded in American English. Its parameters do not indicate the name of a certain language, but are represented by country code. The advantage of this is that it not only determines the language selection, but also can be different according to the region. For example: English, as the most widely used language, has certain differences in many different regions. To determine whether the current system supports language resources in a certain region, you can select the correct processing method by calling the return value of the isLanguageAvailable() method and selecting the correct processing method based on the description of the return value. Making applications that use certain brilliant functions more robust is a technical link that needs to be considered throughout the entire development process. Below are some application examples (androidres):

())
())
(new Locale("spa", "ESP")))

If the return value is "TextToSpeech.LANG_COUNTRY_AVAILABLE", it means that the selected region is included in the current TTS system. If the TTS entity has been created in the system, the isLanguageAvailable() method can be used instead of the Start "ACTION_CHECK_TTS_DATA" intent detection. When no available resource matches the specified parameter cannot be found, the result of "TextToSpeech.LANG_MISSING_DATA" will be returned. Here are two other examples that return other different status information:

(Locale.CANADA_FRENCH))
(new Locale("spa"))

The return values ​​of both statements are "TextToSpeech.LANG_AVAILABLE". The first is to detect whether the current system supports Canadian language. Since the system cannot find the French branch of this region in the resource library, its meaning is to only support this language (French), and not support the language branch of the current Unigroup v4.0 download region.

In addition, compared with the above forced users to apply predetermined voice settings, it is more recommended to use the () method to select a suitable language library based on the user's default regional settings.

Specific methods to execute Speak:

According to the above introduction, the initialization and parameter configuration of TextToSpeech are basically implemented. The following is an application example about an alarm clock. Using the Speak() method, you can directly exert powerful voice functions in the application. Yes, it's that simple to use:

String myText1 = "This Translation is from androidRes";
String myText2 = "I hope so, because it's time to wake up.";
(myText1, TextToSpeech.QUEUE_FLUSH, null);
(myText2, TextToSpeech.QUEUE_ADD, null);

How TTS Engine works:

Each independent application can create a TTS entity individually, and the voice message queues (queues) they need to perform are uniformly managed and voice synthesised by TTS Engine.

Explanation of nouns:

synthesize [snθsaz] DJ ['snθsaz] KK: to produce sounds, music or speech using electronic equipment
utterances [trns] DJ [trns] KK: Speech style, pronunciation/intonation.

Each independent TTS instance manages the priority and order of voice message queue requests, etc. When the Speak() method is called with reference to "TextToSpeech.QUEUE_FLUSH", the task running in the current instance will be interrupted (it can also be understood as clearing the current voice task and executing a new queued task). The pronunciation task citing the "TextToSpeech.QUEUE_ADD" tag will be added after the current task queue.

Associate Stream Type for Voice Tasks:

In the Android operating system, all AudioStream tasks are implemented through the AudioManager class, and it will change the voice playback mode for different StreamTypes. StreamType can be understood as the voice playback attribute, which is an application solution configured by the user in the system according to his own needs. If you clearly classify all voice tasks, you can conveniently manage the attributes of tasks of the same category. Based on the previous Alarm Clock example, replace the last Null parameter of the Speak() method with a numeric value with actual meaning. The type of this parameter is HashMap. If you want to set the current Stream Type to the Alarm type in the system, make a slight change to the previous example:

HashMap myHashAlarm = new HashMap();
(.KEY_PARAM_STRE AM,
(AudioManager.STREAM_ALARM));
(myText1, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
(myText2, TextToSpeech.QUEUE_ADD, myHashAlarm);

Completion Callback that applies voice function:

Speak() in TTS is an asynchronous call. Regardless of applying QUEUE_FLUSH or QUEUE_ADD as parameters, you can listen for the completion status of the current task by defining the Listener. This method can be used to append some additional operations after Speak() is executed. Next In the following example, after completing the second Speak() method call, use the OnUtteranceCompletedListener interface to call other methods:

(this);
(.KEY_PARAM_STRE AM,
(AudioManager.STREAM_ALARM));
(myText1, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
(.KEY_PARAM_UTTE RANCE_ID,
"end of wakeup message ID");
// myHashAlarm now contains two optional parameters
(myText2, TextToSpeech.QUEUE_ADD, myHashAlarm);

Below is the code that defines Listener, similar to the method of listening to press the CuteFTP Home 8.3.3.0054 download button or other View Events. Here, the HashMap parameter in Speak() will be passed into Listener as the basis for judging the conditions:

public void onUtteranceCompleted(String uttId) {
if (uttId == "end of wakeup message ID") {
playAnnoyingMusic();
}
}

"Bake" the current real-time voice data:

When you see the word baking, you will remind you of fragrant bread. Software development should focus on whether it can maximize resource reuse, especially for mobile application platforms with limited resources. So how can such a luxurious application as TTS be used more efficiently? Let's experience a more exciting feature this time than baking bread, saving the Audio Stream output by TTS Engine as a permanent audio file in the current storage space (SDCard). This will enable the fast playback function for certain voice content that needs to be played repeatedly, thereby achieving the internationally advocated "emission reduction" purpose. Save as much as possible! In the following example, using the synthesizeToFile method through TTS, the synthesize Stream is saved in the address specified by the parameter.

HashMap myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/";
(.KEY_PARAM_UTT ERANCE_ID, wakeUpText);
(wakuUpText, myHashRender, destFileName);

After completing the above operations, you will receive a system completion notification, and you can play it through methods like other audio resources. But this is contrary to the application process of TextToSpeech. You can store the voice and text descriptions of the just-out voice resources in the TTS library through the addSpeech() method.

(wakeUpText, destFileName);

In the current TTS Instance, any call that uses the Speak() method to perform the same content will reuse the audio file just generated. If the resource is lost or the storage device such as SDCard is removed, the system will synthesize the specified voice content through TTS Engine again.

(wakeUpText, TextToSpeech.QUEUE_ADD, myHashAlarm);

Recycling TTS:

After confirming that the application no longer needs TTS related functions, you can call shutDown() in the OnDestroy() method of the Activity to release the resources occupied by the current TTS entity.

For more information about Android related content, please check out the topic of this site:Android database operation skills summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.