import java.io.UnsupportedEncodingException; 

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.LexiconListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechListener;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.util.ContactManager.ContactListener;
import com.iflytek.cloud.util.UserWords; public class MainActivity extends Activity implements OnClickListener { private static String TAG = "IatDemo";
// 语音听写对象
private SpeechRecognizer mIat;
// 听写结果内容
private EditText mResultText;
// 用户词表下载结果
private String mDownloadResult = ""; private Toast mToast; private int ret; private Button startRecording; private String voiceResult = ""; private TextView text_result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIat = SpeechRecognizer.createRecognizer(this, mInitListener); mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
text_result = (TextView) this.findViewById(R.id.text_result);
startRecording = (Button) this.findViewById(R.id.start_recording);
startRecording.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void getVoice() {
SpeechUtility.createUtility(MainActivity.this, "appid=540593c3");
// 设置参数
setParam();
// 不显示听写对话框
ret = mIat.startListening(recognizerListener); } public void setParam() { mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); // 设置语音前端点
mIat.setParameter(SpeechConstant.VAD_BOS, "4000");
// 设置语音后端点
mIat.setParameter(SpeechConstant.VAD_EOS, "1000");
// 设置标点符号
mIat.setParameter(SpeechConstant.ASR_PTT, "1");
// 设置音频保存路径
mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/xunfei");
} /**
* 初始化监听器。
*/
private InitListener mInitListener = new InitListener() { @Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code == ErrorCode.SUCCESS) {
Toast.makeText(MainActivity.this, "init success", 0).show();
}
}
}; /**
* 用户词表下载监听器。
*/
private SpeechListener downloadlistener = new SpeechListener() { @Override
public void onData(byte[] data) {
try {
if (data != null && data.length > 1)
mDownloadResult = new String(data, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} } @Override
public void onCompleted(SpeechError error) {
if (error != null) { } else if (TextUtils.isEmpty(mDownloadResult)) { } else {
mResultText.setText("");
UserWords userwords = new UserWords(mDownloadResult.toString());
if (userwords == null || userwords.getKeys() == null) { return;
}
for (String key : userwords.getKeys()) {
mResultText.append(key + ":");
for (String word : userwords.getWords(key)) {
mResultText.append(word + ",");
}
mResultText.append("\n");
} }
} @Override
public void onEvent(int eventType, Bundle params) {
} }; /**
* 听写监听器。
*/
private RecognizerListener recognizerListener = new RecognizerListener() { @Override
public void onBeginOfSpeech() {
Toast.makeText(MainActivity.this, "begin speech", 0).show();
} @Override
public void onError(SpeechError error) {
Toast.makeText(MainActivity.this, error.getErrorDescription(), 0).show();
} @Override
public void onEndOfSpeech() {
Toast.makeText(MainActivity.this, "end", 0).show();
} @Override
public void onEvent(int eventType, int arg1, int arg2, String msg) {
Toast.makeText(MainActivity.this, "on event", 0).show();
} @Override
public void onResult(RecognizerResult results, boolean isLast) {
String text = JsonParser.parseIatResult(results.getResultString());
voiceResult = voiceResult + text;
if (isLast) {
text_result.setText(voiceResult);
}
} @Override
public void onVolumeChanged(int volume) { } }; @Override
protected void onResume() {
// SpeechUtility.getUtility().checkServiceInstalled();
super.onResume();
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_recording:
getVoice();
break;
}
} }

讯飞语音识别Android-Demo的更多相关文章

  1. android 讯飞语音识别(离线)注意事项

    讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...

  2. 微信小程序语音与讯飞语音识别接口(Java)

    项目需求,需要使用讯飞的语音识别接口,将微信小程序上传的录音文件识别成文字返回 而微信小程序上传的文件格式是silk的,而讯飞接口能识别wav 格式的文件,所以需要将小程序上传的silk文件转成wav ...

  3. 微信小程序语音与讯飞语音识别接口(Java),Kronopath/SILKCodec,ffmpeg处理silk,pcm,wav转换

    项目需求,需要使用讯飞的语音识别接口,将微信小程序上传的录音文件识别成文字返回 首先去讯飞开放平台中申请开通语音识别功能 在这里面下载sdk,然后解压,注意appid与sdk是关联的,appid在初始 ...

  4. 【Unity】讯飞语音识别SDK

    1.进入讯飞官网,注册帐号,进入控制台,创建新应用UnityXunfeiDemo,平台选Android.在当前应用这点下载SDK,添加AI能力(添加新服务),选择语音听写,即可下载安卓SDK(下称讯飞 ...

  5. 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino

    大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...

  6. 关于讯飞 使用android SDK出现21001错误码的分析

    21001,没有安装语音组件1.有没有使用SpeechUtility.createUtility()设置appid2.有没有将libmsc.so放到工程中,jar包有Msc.jar.Sunflower ...

  7. Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法

    本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...

  8. MAC电脑如何将常规视频中音频提取出来(转换格式并调整采样频率),并利用讯飞语音识别文字

    1.下载好相关视频 2.选中需要提取视频,鼠标右键找到「编码所选视频文件」 3.设置中,下拉选择「仅音频」,点击继续 4.找到已提取成功的音频,鼠标右键或快捷键「command + I」,显示简介.默 ...

  9. 关于讯飞语音SDK开发学习

    前奏,浑浑噩噩已经工作一年多,这一年多收获还是挺多的.逛园子应该有两年多了,工作后基本上是天天都会来园子逛逛,园子 里还是有很多牛人写了一些不错的博客,帮我解决很多问题.但是一直没写过博客,归根到底一 ...

随机推荐

  1. Linux学习-使用传统程序语言进行编译的简单范例

    单一程序:印出 HelloWorld 编辑程序代码,亦即原始码 [root@study ~]# vim hello.c <==用 C 语言写的程序扩展名建议用 .c #include <s ...

  2. CodeForces 567F DP Mausoleum

    本着只贴代码不写分析的题解是在耍流氓的原则,还是决定写点分析. 思路很清晰,参考的官方题解,一下文字仅对题解做一个简要翻译. 题意: 有1~n这n个数,每个数用两次.构成一个长为2n的序列,而且要求序 ...

  3. 细嚼慢咽 Mongoose 5

    此文已由作者黄锴授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 由于Mongoose一直没有中文文档,加上现在市面上充斥着太多"快速上手",很多中文 ...

  4. 求1+2+...+n 【微软面试100题 第十二题】

    题目要求: 要求不能使用乘除法,for/while/if/else/switch/case等关键字以及条件判断语句(A?B:C). 参考资料:剑指offer第46题 题目分析: 方法1:利用类的静态成 ...

  5. Java项目引入eclipse注意事项

    我以前也搞过java,后转前端,接触html+css+js时间比较多,所以java后端也忘了差不多.最近负责公司的邮件系统项目,项目是java语言写,项目架构比较复杂,在部署项目的时候,遇到了很多问题 ...

  6. tab选项卡不同样式的效果

    一般的tab选项卡就只能两种样式,一种是选中或者是划过这个选项卡样式,一种是没选中或者没划过选项卡样式. 现在有这种需求,就是选中或划过tab选卡要不同样式.比如tab1选中或者划过是红色,tab2选 ...

  7. 从几率到logisitic函数

    odds 几率,又称事件的优势比.几率和概率的关系如下: o=p1−pp=o1+o Logistic 回归模型的因变量只有 1/0 两种取值.假设在 p 个独立自变量 x1,x2,…,xp 作用下,记 ...

  8. php_strip_whitespace和trim的搭配使用

    在学习kongphp框架时有这么一段代码是为了生成运行时文件的 $runfile = RUNTIME_PATH.'_runtime.php'; if(!is_file($runfile)) { $s ...

  9. 浏览器提示ERR_CONTENT_DECODING_FAILED,Gzip压缩数据无法解压

    最近在页面上有个显示数据表格的功能,数据由后台传给前台JS表格插件.数据格式为JSON 由于数据量很大,就想到用GZIP压缩以后传给前台.压缩前,某个表格的数据量达到3M多,用GZIP压缩后就200K ...

  10. 关于php ‘==’ 与 '===' 遇见的坑

    两个的区别所有PHPer都知道, 今天在遍历 xmlNode时,自己写的代码就碰坑了 想遍历xmlNode为数组 得到的xmlNode为 想要把所有的simpleXmlElement对象都遍历转成数组 ...