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可以查询需要的语音库是否存在,不在的户运行用户选择下载需要的语音库:

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可以开始工作,否则可以通知用户下载指定的语音库。

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)

例子中回调函数定义如下:

// 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中随机选择一句读出:

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表示添加到队列的后面。



 


 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值