Usage Analysis of Automatic Reading TTS Developed by Android

  • 2021-07-26 08:54:32
  • OfStack

This paper illustrates the usage of Android reading TTS automatically. Share it for your reference, as follows:

TextToSpeech is simply called TTS, which is an important new feature since Android version 1.6. Converts the specified text into audio output in a different language. It can be easily embedded into games or applications to enhance the user experience.

Before explaining TTS API and how to apply this functionality to your real project, get a glimpse of the TTS engine.

General understanding of TTS resources:

TTS engine relies on several major languages currently supported by Android Platform: English, French, German, Italian and Spanish 5 (without our great Chinese for the time being, at least google scientists have not played Chinese to perfection, and it is natural that it is easy before it is difficult.) TTS can convert text into speech output in any of the above five languages at will. 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 (it can be seen that Google's style of doing things is really meticulous, and it is estimated that another reason why Google does not add Chinese is that there are too many dialects in Chinese).

To support such a huge amount of data, TTS engine adopts preloading method for resource optimization. Extract the corresponding resources from the library according to the parameter information of Series 1 (the usage of parameters will be described in detail later) and load them into the current system.

Although most devices loaded with Android operating system provide TTS function through this engine at present, the storage space of some devices is very limited, which affects the maximum function of TTS, which is regarded as a bottleneck at present. Therefore, the detection module is introduced in the development, so that the application or game using this technology can be optimized and adjusted for different devices, so as to avoid the limitation of this function, which affects the use of the whole application. It is safer 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();
checkIntent.setAction(TextToSpeech.Engine.ACTION_C HECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

If the current system allows the creation of a rainbow font library of "android. speech. tts. TextToSpeech" to download Object, it means that the support of TTS function has been provided, and the flag of "CHECK_VOICE_DATA_PASS" will be given in the returned result. If the system does not support this function, the user can choose whether to load this function, so that the device can support the speech function "Multi-lingual Talking" for outputting multiple languages. "ACTION_INSTALL_TTS_DATA" intent introduces users to the TTS download interface in Android market. After the download is complete, the installation will be completed automatically. Here is the complete code to implement this 1 process (androidres):


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

Both the TextToSpeech entity and the OnInitListener need to reference the Context of the current Activity as the construction parameter. The purpose of OnInitListener () is to notify the system that the current 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 vivid experience about applying this function, and the translation results were directly output through the voice of five different languages. The method of loading the language is very simple:


mTts.setLanguage(Locale.US);

The above code indicates that the current TTS entity loads American English. The parameters do not indicate the name of a language, but are represented by the country code, which has the advantage of not only determining the choice of language, but also making differences according to different regions. For example, English, as the most widely used language, has certain differences in many different regions. To judge whether the current system supports language resources in a certain region, we can call the return value of isLanguageAvailable () method and choose the correct processing mode according to the description of the return value. Making applications with some gorgeous functions more robust is a technical link to be considered throughout the development process. Below are some application examples (androidres):


mTts.isLanguageAvailable(Locale.UK))
mTts.isLanguageAvailable(Locale.FRANCE))
mTts.isLanguageAvailable(new Locale("spa", "ESP")))

If the return value is "TextToSpeech.LANG_COUNTRY_AVAILABLE", the selected locale is included in the current TTS system. If an TTS entity has been created in the system, the isLanguageAvailable () method can be used instead of the Start "ACTION_CHECK_TTS_DATA" intent detection. A result of "TextToSpeech. LANG_MISSING_DATA" is returned when no available resource can be found matching the specified parameters. Here are two other examples of returning other different status information:


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

The return value of both statements is "TextToSpeech.LANG_AVAILABLE". The first is to check whether the current system supports Canadian *****. Because the system cannot find the French branch of this area in the resource library, it means that it only supports this language (French), but does not support the language branch of the current Purple Light v4.0 download area.

In addition, the Locale. getDefault () method is more recommended to select the appropriate language library according to the user's default locale setting than forcing the user to apply the predetermined voice setting above.

The specific method of executing Speak:

According to the above introduction, the initialization and parameter configuration of TextToSpeech are basically realized. The following is an application example of alarm clock. Using Speak () method, you can directly play a powerful voice function in the application program. Yes, it is as simple as that:


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

How TTS Engine works:

Each stand-alone application can create an TTS entity individually, and the voice message queuing (Queue) they need to perform is managed by TTS Engine and speech synthesis.

Explanation of nouns:

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

Each independent TTS instance manages the priority and order of voice message queuing requests and so on. When the Speak () method is called with reference to "TextToSpeech. QUEUE_FLUSH", the task that the current instance is running is interrupted (also understood as clearing the current voice task and executing a new queued task instead). Pronunciation tasks referencing the "TextToSpeech. QUEUE_ADD" tag will be added to the current task queue.

Associate Stream Type for voice tasks:

In the Android operating system, all AudioStream tasks are implemented through the AudioManager class, which will change the voice playback mode for different StreamType. StreamType can be understood as the playback attribute of voice, which is the application scheme configured by users in the system according to their own needs. If voice tasks are clearly classified, it is convenient to 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 value that has a practical 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();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STRE AM,
String.valueOf(AudioManager.STREAM_ALARM));
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, myHashAlarm);

Completion Callback with voice function:

Speak () in TTS is an asynchronous call. QUEUE_FLUSH or QUEUE_ADD can be used as parameters to monitor the completion status of the current task by defining Listener. You can use this method to append one additional operation after Speak () executes. In the following example, after the second Speak () method call is completed, the OnUtteranceCompletedListener interface is used to call other methods:


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

Below is the code that defines Listener, similar to listening for pressing 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 condition:


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

"Bake" current real-time voice data:

When you see the word baking, you will think of delicious bread. Software development should pay attention to whether the resources can be reused to the maximum extent, especially for the mobile phone application platform with limited resources. So how can we use resources more efficiently for such a luxurious application as TTS? This time, experience more exciting functions than baking bread, and save Audio Stream output from TTS Engine as a permanent audio file in the current storage space (SDCard). In this way, some voice contents that need to be played repeatedly can be played back quickly, thus realizing the purpose of "emission reduction" advocated by the international community. In the following example, the synthesized speech Stream is saved in the address specified by the parameter using the synthesizeToFile method through TTS.


HashMap myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTT ERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakuUpText, myHashRender, destFileName);

After completing the above operations, we will receive the completion notification of the system, and at the same time, we can play it through android. media. MediaPlayer method like other audio resources. However, this is contrary to the application process of TextToSpeech. The voice resources just output can be stored in TTS library by addSpeech ().


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

0

In the current TTS Instance, any call that makes the same use of the Speak () method 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 speech content through TTS Engine again.


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

1

Recycling TTS:

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

For more readers interested in Android related contents, please check the topics on this site: "Summary of Android Database Operation Skills", "Summary of activity Operation Skills for Android Programming", "Summary of Android File Operation Skills", "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of Android View View Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: