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.");
}
}