Android ApiDemos示例解析(45):App->Text-To-Speech
从Android1.6(API Level 4)开始,Android平台开始支持文字到语音(TTS)功能,也就是“合成语音”,支持以声音方式读出文字。
目前Android TTS可以支持多种语言:English, French, German, Italian ,Spanish 等,也有公司提供了用于Android平台的中文TTS Engine。
TTS Engine 在读出文字前,需要知道使用哪种语言,比如“Paris”的发音,英语和法语发音就不同。因此TTS使用的语音库是跟语言相关的。在使用TTS之前需要调入相应的语音库。
尽管Android平台支持TTS,但具体的设备可能不自带某种语言的语音库,Android TTS可以查询需要的语音库是否存在,不在的户运行用户选择下载需要的语音库:
[java]
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
如果需要的语音库存在,则result code为CHECK_VOICE_DATA_PASS,表示TTS可以开始工作,否则可以通知用户下载指定的语音库。如果需要的语音库存在,则result code为CHECK_VOICE_DATA_PASS,表示TTS可以开始工作,否则可以通知用户下载指定的语音库。
[java]
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);
}
}
}
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 介绍了TTS的一般用法,可以随机读出一个字符串数组中的文字。
TextToSpeech的构造函数 ,第一个参数可以使用当前Activity,第二个参数为TTS 初始化后回调函数onInit.
public TextToSpeech(Context context, TextToSpeech.OnInitListener listener)
例子中回调函数定义如下:
[java]
// 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 english.
// Note that a language may not be available, and the result will indicate this.
int result = mTts.setLanguage(Locale.US);
// Try this someday for some interesting results.
// int result mTts.setLanguage(Locale.FRANCE);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user.
sayHello();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
// 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 english.
// Note that a language may not be available, and the result will indicate this.
int result = mTts.setLanguage(Locale.US);
// Try this someday for some interesting results.
// int result mTts.setLanguage(Locale.FRANCE);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user.
sayHello();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
status为TextToSpeech.SUCCESS表示TextToSpeech Engine成功初始化,然后设置语言为英语,如果本机带有英语的语音库,就可以开使使用speak来读出文本了。
下面代码从数组HELLOS中随机选择一句读出:
[java]
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
每个TTS Engine 使用一个队列用于合成语音,TextToSpeech.QUEUE_FLUSH意味着清除队列中内容,将本句放在队列的首位, 而TextToSpeech.QUEUE_ADD表示添加到队列的后面。
作者:mapdigit
补充:移动开发 , Android ,