从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表示添加到队列的后面。
分享到:
相关推荐
NULL 博文链接:https://kakukemeit.iteye.com/blog/1806388
android apidemos代码的示例解析,讲的挺全的
最新版ApiDemos Android SDK 中带有很多例子,其中ApiDemo 详细介绍了Android 平台主要API,分成了 · App · Content · Graphics · Media · OS · Text · Views 几个大类,每个大类又分为几个小类,...
Android ApiDemos apk, 包括Android presentation双屏异显 demon, App>Activity>Presentation
最新Android apidemos
API Demos 是 Google 为了 Android 开发者所提供的一个 Android API 合集,其中包含了很多的 API 范例,同时遵循了良好的代码规范,是一个值得开发者研究和学习的典型。android的ApiDemos,需要解压缩后使用。
android APIdemos,android 源码简单使用demos
该Api Demos 源码是基于Android6.0 ,Eclipse下的,可以直接使用Android studio 导入工程即可。
Android 系统原生应用API示例程序集合,是初学Android 开发的重要参考资料 https://github.com/longtaoge
Oracle Solaris 9 -man pages section 6: Demos-18
Android官网ApiDemos源码 供大家学习参考之用
Oracle Solaris 9 man pages section 6: Demos-18
Oracle Solaris 8 man pages section 6: Demos-38
AndroidDemosStudio AndroidDemos ------- Android Studio
api-ai-ios-sdk_demos api-ai-ios-sdk_demos的演示应用语音或文本>目的/操作> Json “为应用程序,设备和网络构建智能语音界面” 基于 获取示例以构建遇到问题因此将其提取到独立项目中在创建帐户,然后使用设置示例...
API-Demos-api-16-android4.1:android api api
android示例源码 有需要的可下载看一下吧
Android ApiDemos2.1版本,sdk自带的sample。