Automatic reading TTS usage analysis for Android development _android

Source: Internet
Author: User
Tags time zones locale message queue numeric value

This example describes how Android automatically reads TTS usage. Share to everyone for your reference, specific as follows:

Texttospeech, TTS, is a more important new feature since the Android 1.6 version. Converts the specified text into a different language audio output. It can be easily embedded in a game or application to enhance the user experience.

Before you explain the TTS API and how to apply this functionality to your actual project, you have an initial understanding of the TTS engine.

Broad understanding of TTS resources:

The TTS engine relies on the main languages supported by the current Android platform: Chinese, French, German, Italian, and Spanish (for the time being, we don't have our great Mandarin, at least Google's Scientists have not yet played the Chinese to the point of perfection, first easy after the difficult is also taken for granted. TTS can convert text freely into speech output in any of the five languages above. At the same time, for individual language versions it will depend on different time zones, for example: For Chinese, you can export both American and English versions in TTS (which shows that Google's style of doing things is really meticulous and The other reason why Google does not join the Chinese language is that there are too many dialects in Chinese.

To support such a large amount of data, the TTS engine takes a preload approach to resource optimization. The corresponding resource is extracted from the library and loaded into the current system, based on a series of parameter information (the usage of the parameters will be described in detail behind).

Although most of the devices currently loaded with the Android operating system provide TTS functionality through this engine, it is a current bottleneck because of the limited storage space of some devices that affect TTS's inability to maximize its functionality. To this end, the development of the detection module, so that the use of this technology applications or games for different devices can be optimized to adjust, so as to avoid the limitations due to this feature, affect the use of the entire application. It is safer to let the user choose whether there is enough space or need to load the resource, and below 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 "Android.speech.tts.TextToSpeech" rainbow notation font to download object, the support for TTS functionality is provided and the "Check_voice_data_pass" is given in the Test return results The tag. If the system does not support this feature, then the user can choose whether to load this feature, so that the device to support the output of multi-language voice function "multi-lingual talking." The "Action_install_tts_data" intent introduces the user to the TTS download interface in the Android market. When the download completes, the installation is completed automatically, and the following is the complete code to implement the 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 refer to the context of the current activity as the construction parameter. The use of Oninitlistener () is to notify the system that the current TTS engine has been loaded and is in a usable state.

Set language parameters according to requirements:

As early as the Google I/O Conference, the authorities gave a vivid experience of applying this feature, translating the results directly through the voice output of five different languages. The way to load a language is simple:

Mtts.setlanguage (locale.us);

The top code indicates that the current TTS entity loads American English. Its arguments do not indicate the name of a language, but rather use the country code to indicate that the benefit is not only to determine the choice of the language, but also to distinguish between different regions. For example: English as the most widely used language, in a number of different regions have a certain difference. To determine whether the current system supports language resources for a region, you can choose the correct processing by calling the return value of the Islanguageavailable () method, based on the description of the return value. Making applications that use some gorgeous features more robust is a technical step that will be considered throughout the development process. Below are some examples of applications (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 region is included in the current TTS system. If you have already created a TTS entity on your system, you can use the Islanguageavailable () method instead of the Start "action_check_tts_data" intent detection. The result of "Texttospeech.lang_missing_data" will be returned when any available resources cannot be found to match the specified parameters. Two other examples of returning different state information are given below:

Mtts.islanguageavailable (locale.canada_french))
mtts.islanguageavailable (New Locale ("Spa"))

The return value of two statements is "texttospeech.lang_available". The first is to detect if the current system supports add-in, because the system cannot find the French branch of the region in the repository, meaning it only supports this language (French) and does not support the language branch of the current Violet v4.0 download area.

In addition, it is more recommended to use the Locale.getdefault () method to select the appropriate language library based on the user's default locale, compared to forcing the user to apply predetermined voice settings.

Specific ways to perform speak:

According to the above introduction, the basic implementation of Texttospeech initialization and parameter configuration. Here is an example of an application of the alarm clock, using the Speak () method to play a powerful voice function directly in the application. Yes, it's as simple as this:

String myText1 = "This translation be 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);

The working principle of TTS engine:

Each stand-alone application can create a single TTS entity, and the voice message queues (queue) They need to perform are unified by TTS engine management and speech synthesis.

Noun Explanation:

synthesize [snθsaz] DJ [' snθsaz] kk:to produce sounds, music or speech using electronic equipment (stereo) synthesis
utterances [Trns] DJ [trns] KK: Way of speaking, voice/intonation.

Each individual TTS instance manages the priority and order of voice message queue requests. When a reference to "Texttospeech.queue_flush" calls the Speak () method, the current instance's running task is interrupted (or it can be understood to purge the current voice task and perform a new queue task). The pronunciation task that references the "Texttospeech.queue_add" label is added after the current task queue.

To associate the stream Type for a speech task:

All the Audiostream tasks in the Android operating system are implemented through the Audiomanager class, and it will change the playback mode of the voice for different streamtype. Streamtype can be understood as the playback properties of speech, this property is the user according to their needs in the system configuration of the application scheme. If you classify voice tasks clearly, you can easily manage the properties 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 that has an actual meaning. The type of this parameter is HashMap, and 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 invocation that can be used to monitor the completion status of the current task, either by applying Queue_flush or Queue_add as a parameter. You can use this method to append some additional actions after the speak () execution. In the following example, when the second speak () method call is completed, another method is invoked using the Onutterancecompletedlistener interface:

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 the method of listening to the CuteFTP home 8.3.3.0054 download button or other view events. The HashMap parameters in the Speak () will be passed into the listener as a criterion for judging the conditions:

public void onutterancecompleted (String uttid) {
if (Uttid = "End of wakeup message ID") {
playannoyingmusic ();
}
}

"Baking" Current real-time voice data:

See baking two words, it will make people think of delicious bread. Software development should focus on whether the resources can be reused to the maximum extent, especially for mobile phone application platforms with limited resources. So how can you use resources more efficiently with such a luxurious application of TTS? This time together to experience a more exciting function than baking bread, save the TTS engine output audio stream as a permanent audio file in the current storage space (SDcard). This can be repeated playback of some of the voice content to achieve a fast playback function, so as to achieve international advocacy of "emission reduction" purposes, can save the province! In the example below, the synthesized voice stream is saved in the address specified by the parameter by using the Synthesizetofile method of 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);

When you are done, you will receive a complete notification of the system, and you can play it through the Android.media.MediaPlayer method like any other audio resource. However, this is contrary to the Texttospeech application process, you can store the speech resources just output through the Addspeech () method of their voice and text description together in the TTS library.

Mtts.addspeech (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 a resource is lost or a storage device such as SDcard is removed, the system will again synthesize the specified speech content via TTS engine.

Mtts.speak (Wakeuptext, Texttospeech.queue_add, myhashalarm);

Recycle TTS:

When you determine that the application does not need TTS-related functionality, you can call shutdown () in the OnDestroy () method of the activity to release the resources that are occupied by the current TTS entity.

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.