讯飞语音 + 新手指南 Release1.0 2013 年 5 月 14 日
版权声明版权所有 2013,, 保留所有权利 商标说明的产品是专有 在提及其他公司及其产品时将使用各自公司所拥有的商标, 这种使用的目的仅限于引用 本文档可能涉及的专利 ( 或正在申请的专利 ) 商标 版权或其他知识产权, 除非得到的明确书面许可协议, 本文档不授予使用这些专利 ( 或正在申请的专利 ) 商标 版权或其他知识产权的任何许可协议 不作保证声明不对此文档中的任何内容作任何明示或暗示的陈述或保证, 而且不对特定目的的适销性及适用性或者任何间接 特殊或连带的损失承担任何责任 本手册内容若有变动, 恕不另行通知 本手册例子中所用的公司 人名和数据若非特别声明, 均属虚构 未得到明确的书面许可, 不得为任何目的 以任何形式或手段 ( 电子的或机械的 ) 复制或传播手册的任何部分 保密声明本文档 ( 包括任何附件 ) 包含的信息是保密信息 接收人了解其获得的本文档是保密的, 除用于规定的目的外不得用于任何目的, 也不得将本文档泄露给任何第三方 本软件产品受最终用户许可协议 (EULA) 中所述条款和条件的约束, 该协议位于产品文档和 / 或软件产品的联机文档中, 使用本产品, 表明您已阅读并接受了 EULA 的条款 版权所有 Copyrights Anhui USTC iflytek CO., LTD. 第 2 页
1 概述 讯飞语音 + 是科大讯飞公司向开发者提供的语音开发包 本文讲解 讯飞语音 + SDK 集成使用方法, 默认读者已经掌握 Android 应用程序开发知识 注 :1 目前的版本支持语音合成 语音识别 语音理解, 声纹识别和唤醒将在后续版 本开放, 敬请关注 行配置 : 2 如果需要混淆代码, 为了保证 sdk 的正常使用, 需要在 proguard.cfg 加上下面三 -dontwarn com.iflytek.speech.** -keepattributes Signature -keep class com.iflytek.speech.**{*; 2 Speech API 概述 应用程序若使用 讯飞语音 + 提供的语音能力, 需要将 讯飞语音 + 开发包中 SpeechApi.jar( 见 SDK 目录 ) 集成到应用程序中去 Speech API 接口的使用方法, 请参考 Speech API 帮助手册 第三方应用程序集成使用 讯飞语音 + 开发包逻辑关系图 : 第三方 App 讯飞语音 +App (SpeechService.apk) SpeechApi BindService 3 搭建开发环境 [1] 在 Eclipse 中建立你的 Android 工程 [2] 将开发工具包中 lib 目录下的 SpeechApi.jar 复制到新建工程的 libs 目录中 ( 如下图所示 ) 第 3 页
[3] 右键单击工程, 选择 Build Path 中的 Configure Build Path..., 选中 Libraries 这 个 tab, 并通过 Add Jars... 导入工程 libs 目录下的 SpeechApi.jar 文件 ( 如下图所示 ) [4] 在你需要使用讯飞语音 + 服务的文件中导入相应的类 import com.iflytek.speech.speechrecognizer; 4 在代码中使用开发工具包 [1] 语音识别 使用示例如下图所示 // 检测是否安装了讯飞语音服务 if (SpeechUtility.getUtility(this).queryAvailableEngines() 第 4 页
== null SpeechUtility.getUtility(this). queryavailableengines().length<= 0) { // 下载安装或者本地安装, 请参照 demo 代码 // 设置申请的应用的 appid SpeechUtility.getUtility(this).setAppid(" 应用 appid"); // 初始化识别对象 SpeechRecognizermIat = new SpeechRecognizer(this, minitlistener); // 转写会话 miat.setparameter(speechconstant.params, "asr_ptt=1"); miat.startlistening(mrecognizerlistener); // 转写回话停止 miat.stoplistening(mrecognizerlistener); // 取消 miat.cancel(mrecognizerlistener); // 转写回调 RecognizerListener mrecognizerlistener = new RecognizerListener.Stub() { publicvoid onvolumechanged(int v) throws RemoteException { // 录音音量回调 publicvoid onresult(final RecognizerResult result, boolean islast) throws RemoteException { // 结果回调 publicvoid onerror(int errorcode) throws RemoteException { // 错误回调 publicvoid onendofspeech() throws RemoteException { 第 5 页
// 录音结束回调 publicvoid onbeginofspeech() throws RemoteException { // 录音启动回调 ; 也可以调用我们定义的语音输入窗口, 如下 : 1) 通过以下两个 action 可以调用窗口 com.iflytek.speech.action.voiceinput com.iflytek.speech.action.voiceisearch 2) 可以设置弹出窗口的两个按钮的名字 intent.putextra("title_done", " 确定 "); intent.putextra("title_cancel", " 取消 "); // 检测是否安装了讯飞语音服务 Intent intent = new Intent(); // 指定 action 名字 intent.setaction("com.iflytek.speech.action.voiceinput"); intent.putextra(speechconstant.params, "asr_ptt=0"); intent.putextra(speechconstant.vad_eos, "1000"); // 设置弹出框的两个按钮名称 intent.putextra("title_done", " 确定 "); intent.putextra("title_cancel", " 取消 "); startactivityforresult(intent, REQUEST_CODE_SEARCH); data) { protectedvoid onactivityresult(int requestcode, int resultcode, Intent if(requestcode == REQUEST_CODE_SEARCH&& resultcode == RESULT_OK) { // 取得识别的字符串 ArrayList<String> results = data.getstringarraylistextra(recognizerintent.extra_results); String res = results.get(0); EditText editor = ((EditText)findViewById(R.id.iat_text)); 第 6 页
String text = editor.gettext().tostring()+res; editor.settext(text); super.onactivityresult(requestcode, resultcode, data); [2] 语音合成 使用示例如下图所示 // 检测是否安装了讯飞语音服务 if (SpeechUtility.getUtility(this).queryAvailableEngines() == null SpeechUtility.getUtility(this). { queryavailableengines().length<= 0) // 下载安装或者本地安装, 请参照 demo 代码 // 设置申请的应用的 appid SpeechUtility.getUtility(this).setAppid(" 应用 appid"); // 初始化合成对象 SpeechSynthesizermTts = new SpeechSynthesizer(this, mttsinitlistener); // 设置引擎类型 mtts.setparameter(speechconstant.engine_type, "local"); // 设置发音人 mtts.setparameter(speechsynthesizer.voice_name, "xiaoyan"); // 设置语速 mtts.setparameter(speechsynthesizer.speed, "50"); // 设置音调 mtts.setparameter(speechsynthesizer.pitch, "50"); // 开始合成 int code = mtts.startspeaking(text, mttslistener); // 停止 mtts.stopspeaking(mttslistener); // 暂停播放 mtts.pausespeaking(mttslistener); // 恢复播放 mtts.resumespeaking(mttslistener); // 合成回调监听 第 7 页
SynthesizerListener mttslistener = new SynthesizerListener.Stub() { publicvoid onbufferprogress(int progress) throws RemoteException { // 缓冲进度回调 publicvoid oncompleted(int code) throws RemoteException { // 结束回调 publicvoid onspeakbegin() throws RemoteException { // 开始播放回调 publicvoid onspeakpaused() throws RemoteException { // 暂停回调 publicvoid onspeakprogress(int progress) throws RemoteException { // 播放进度回调 publicvoid onspeakresumed() throws RemoteException { // 重新播放回调 ; 5 附录 1. 识别结果说明 json 字段 英文全称 类型 说明 sn sentence number 第几句 ls last sentence boolean 是否最后一句 bg begin number 开始 第 8 页
ed end number 结束 ws words array 词 cw chinese word array 中文分词 w word string 单字 sc socre number 分数 转写结果示例 : {"sn":1,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"w":" 今 天 ","sc":0],{"bg":0,"cw":[{"w":" 的 ","sc":0],{"bg":0,"cw":[{"w":" 天 气 ","sc":0],{"bg":0,"cw":[{"w":" 怎么样 ","sc":0],{"bg":0,"cw":[{"w":" ","sc":0]] 多候选结果示例 : {"sn":1,"ls":false,"bg":0,"ed":0,"ws":[ {"bg":0,"cw":[{"w":" 我想听 ","sc":0], {"bg":0,"cw":[{"w":" 拉德斯基进行曲 ","sc":0,{"w":" 拉得斯进行曲 ","sc":0]] 语法识别结果示例 : {"sn":1,"ls":true,"bg":0,"ed":0,"ws":[ {"bg":0,"cw":[{"sc":"70","gm":"0","w":" 北京到上海 ", {"sc":"69","gm":"0","w":" 天京到上海 ", {"sc":"58","gm":"0","w":" 东京到上海 " ] ] 2. 语义结果说明 请参照 语义理解处理协议 v1 3200.pdf 第 9 页