Android Apidemos Sample Resolution: App->text-to-speech

Source: Internet
Author: User
Tags locale

Starting with Android1.6 (API level 4), the Android platform is beginning to support Text-to-Speech (TTS), which is "synthetic speech", which supports the ability to read text in a sound way.

Currently, Android TTS can support a variety of languages: 中文版, French, German, Italian, Spanish, and the company offers a Chinese TTS Engine for the Android platform.

TTS Engine you need to know which language to use, such as "Paris", and pronounce English and French differently before you read the text. Therefore, the speech libraries used by TTS are language-dependent. You need to call into the appropriate voice library before using TTS.

Although the Android platform supports TTS, the specific device may not have a language library, and Android TTS can query for the existence of the required voice library, and the users who are not running the user chooses to download the desired voice library:

Intent checkintent = new Intent ();     
Checkintent.setaction (TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);     
Startactivityforresult (Checkintent, My_data_check_code);

If the desired voice library exists, result code is Check_voice_data_pass, which means that TTS can begin to work, or you can notify the user to download the specified voice library. If the desired voice library exists, result code is Check_voice_data_pass, which means that TTS can begin to work, or you can notify the user to download the specified voice library.

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) {
Success, 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);
}
}
}

Texttospeechactivity introduces the general usage of TTS, and can randomly read the text in an array of strings.

Texttospeech constructor, the first parameter can use the current activity, and the second parameter is the callback function OnInit after TTS initialization.

Public Texttospeech (context, Texttospeech.oninitlistener listener)

In the example, the callback function is defined as follows:

Implements Texttospeech.oninitlistener.
public void onInit (int status) {
Status can be either Texttospeech.success or Texttospeech.error.
if (status = = Texttospeech.success) {
Set preferred language to US 中文版.
Note that a language may is available, and the result would indicate this.
int result = Mtts.setlanguage (locale.us);
Try This someday a for some interesting results.
int result Mtts.setlanguage (locale.france);
if (result = = Texttospeech.lang_missing_data | |
result = = texttospeech.lang_not_supported) {
Lanuage the data is missing or the language are not supported.
LOG.E (TAG, "Language is not available.");
} else {
Check the documentation for other possible result codes.
For example, the language may is available for the locale,
But not for the specified country and variant.
The TTS engine has been successfully initialized.
Allow the "User to press" button for the app to speak again.
Magainbutton.setenabled (TRUE);
Greet the user.
SayHello ();
}
} else {
Initialization failed.
LOG.E (TAG, "could not initialize Texttospeech.");
}
}

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.