從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