Browse » Home » Resource, Tutorial » Blog article: 开发Android APPs的语音功能

让你的程序开口说话,没错!接下来讲解如何通过第三方资源为你的应用程序实现语音功能。这个语音库的名字是Text-To-Speech (以下简称TTS),开发者可以通过调用这个这个语音库实现将字符串转http://www.androidres.com/wp-admin/edit-pages.php为声音。

在讲解具体实现方法之前,需要配置Esclipse的设定:

Project > Properties > Java Build Path > Libraries and click on
"Add External JARs..." Then add in the TTS_library_stub.jar file

Download This Library

addingtts_library

使用eSpeak引擎前需要为Emulator添加虚拟SD Card。创建虚拟SD Card的方法


下载tts_market.apk
将这个APK安装到Emulator (启动Emulator的前提下,在CLS中输入”adb install ***.APK “)。

当程序运行后也可以通过参数控制是否需要弹出安装tts_market.apk的提示对话框:

tts_dialog_options

下边通过几行简单的代码来实现调用语音库的方法:

1. 创建一个新的Android项目,可以随意命名为你认为有意义的名字,比如“I am Well-E”等等

2. 应用上边所提到的方法为当前的项目添加Library。

3. 在主程序.java中添加如下代码:

import com.google.tts.TTS;

介于onCreate()和类之间声明空的TTS

private TTS myTts;

在onCreat()中创建TTS的实体

myTts = new TTS(this, ttsInitListener, true);

在onCreat()后边定义一个新函数

private TTS.InitListener ttsInitListener = new TTS.InitListener() {
        public void onInit(int version) {
          myTts.speak("I am Wall E", 0, null);
        }
};

walle

至此可以Run你的程序了,如果顺利的话,第一次启动程序后会首先调用TTS通过Server下载数据,然后手动回到主程序面板重新启动你的程序,听到Emulator发出响亮的声音了吧?

tts_dialog_options_02

创建TTS实体时要求有三个参数:
1. Application Context
2. TTS.InitListener - 监听TTS初始化Event
3. Boolean - 当检测到Emulator或其它测试机没有安装TTs_market.apk时,是否弹出下载
  提示对话框。
TTS实体调用Speak行为时的参数:
1. String - 语音内容
2. Boolean - 是否启用队列模式(0:代表没有列队模式,1:代笔具有先进先出的队列模式)
3. Array - an array of Strings that are parameters for how to speak the text.

更多相关资源:
see the documentation on the TTS
look at other apps in the eyes-free project

查看原文

Tags: ,

Posted in Resource, Tutorial |

Related Posts



Leave a Reply

You must be logged in to post a comment.