Android ApiDemos示例解析(45):App->Text-To-Speech

Android ApiDemos示例解析(45):App->Text-To-Speech
2012-07-05       0  个评论      
收藏     我要投稿

从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
 

id="iframeu2368767_0" src="http://pos.baidu.com/acom?rdid=2368767&dc=2&di=u2368767&dri=0&dis=0&dai=1&ps=4646x195&dcb=BAIDU_UNION_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1458048288556&ti=Android%20ApiDemos%E7%A4%BA%E4%BE%8B%E8%A7%A3%E6%9E%90(45)%EF%BC%9AApp-%3EText-To-Speech%20-%20Android%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF&ari=1&dbv=2&drs=1&pcs=1349x557&pss=1349x4747&cfv=0&cpl=42&chi=1&cce=true&cec=GBK&tlm=1441610972&ltu=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201207%2F138900.html&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGQj1T7s0NW01eJ7TC_tb5Ame0a3ImU_sUSPLYPzQCbWg_Qqs4hm2MumIFQl5yfVrDHz_F6i6LsYN893YieIaW_%26wd%3D%26eqid%3Dbe3d524a0000f5230000000256e80cf8&ecd=1&psr=1366x768&par=1366x706&pis=-1x-1&ccd=24&cja=true&cmi=113&col=zh-CN&cdo=-1&tcn=1458048291&qn=738bb3ef4e0b0c31&tt=1458048291218.30.56.59" width="650" height="100" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
点击复制链接 与好友分享! 回本站首页
上一篇: Android ApiDemos示例解析(44):App->Service->Service Start Arguments Controller
下一篇: Android ApiDemos示例解析(46):App->Voice Recognition
相关文章
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值