语音识别为文字
16lz
2021-01-26
转载自 http://mp.blog.csdn.net/postedit/79120421
1、注册一个http://www.xfyun.cn/用户,申请SDK(免费的),主要是语音听写(就是语音转文字),http://www.xfyun.cn/services/voicedictation
2、下载SDK,注意自己的appid只能用自己的sdk,否则会创建语音识别接口失败
3、创建自己的android工程
参考我的代码 github https://github.com/HungryGoogle/LeeVoice2WordDemo
4、增加权限
5、增加一个识别的activity import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.iflytek.cloud.RecognizerListener;import com.iflytek.cloud.RecognizerResult;import com.iflytek.cloud.SpeechConstant;import com.iflytek.cloud.SpeechError;import com.iflytek.cloud.SpeechRecognizer;import com.iflytek.cloud.SpeechUtility;import java.util.List;public class Voice2Word extends Activity { Button mButton; TextView mResultShowTextView; private String dictationResultStr; private RecognizerListener mRecoListener = new RecognizerListener() { // 听写结果回调接口(返回Json格式结果); // 比如:{"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0.00,"w":"我"}]},{"bg":0,"cw":[{"sc":0.00,"w":"是"}]},{"bg":0,"cw":[{"sc":0.00,"w":"万里长城"}]}]} public void onResult(RecognizerResult results, boolean isLast) { Log.i("leeTest", "RecognizerResult ---> " + results.getResultString()); if (!isLast) { dictationResultStr += results.getResultString() + ","; } else { dictationResultStr += results.getResultString() + "]"; } // 如果是最后一个,进行解析 if (isLast) { // 解析Json列表字符串 Gson gson = new Gson(); List voide2WordsResultList = gson .fromJson(dictationResultStr, new TypeToken>() { }.getType()); String finalResult = ""; for (int i = 0; i < voide2WordsResultList.size() - 1; i++) { finalResult += voide2WordsResultList.get(i) .toString(); } Log.i("leeTest", "last result ---> " + finalResult); mResultShowTextView.setText(finalResult); } } //会话发生错误回调接口 public void onError(SpeechError error) { error.getPlainDescription(true); //获取错误码描述 } @Override public void onVolumeChanged(int i, byte[] bytes) { } //开始录音 public void onBeginOfSpeech() { } //音量值0~30 public void onVolumeChanged(int volume) { } //结束录音 public void onEndOfSpeech() { } //扩展用接口 public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { } }; private SpeechRecognizer mIat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_speach2_word); SpeechUtility.createUtility(Voice2Word.this, SpeechConstant.APPID + "=5931728f"); ButtonListener b = new ButtonListener(); mButton = (Button) findViewById(R.id.button_voice_2_word); mButton.setOnClickListener(b); mButton.setOnTouchListener(b); mButton.setText("按住识别语音"); mResultShowTextView = findViewById(R.id.id_show_voice_2_words_result); // 1 创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener mIat = SpeechRecognizer.createRecognizer(Voice2Word.this, null); // 2 设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类 mIat.setParameter(SpeechConstant.DOMAIN, "iat"); mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); mIat.setParameter(SpeechConstant.ACCENT, "mandarin "); } class ButtonListener implements OnClickListener, View.OnTouchListener { public void onClick(View v) { if (v.getId() == R.id.button_voice_2_word) { Log.d("leeTest", "cansal button ---> click"); } } public boolean onTouch(View v, MotionEvent event) { if (v.getId() == R.id.button_voice_2_word) { if (event.getAction() == MotionEvent.ACTION_DOWN) { Log.d("leeTest", "cansal button ---> ACTION_DOWN"); mButton.setText("录制并识别中..."); //3.开始听写 dictationResultStr = "["; mIat.startListening(mRecoListener); } if (event.getAction() == MotionEvent.ACTION_UP) { Log.d("leeTest", "cansal button ---> ACTION_UP"); mButton.setText("按住识别语音"); mIat.stopListening(); } } return false; } }}
6、增加结果解析类 /** * Created by Li on 2018/1/21. */import java.util.List;/** * 解析语音听写返回结果Json格式字符串的模板类(多重嵌套Json) * 语音识别结果Json数据格式(单条数据): * {"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0.00,"w":"我"}]},{"bg":0,"cw":[{"sc":0.00,"w":"是"}]},{"bg":0,"cw":[{"sc":0.00,"w":"万里长城"}]}]} */public class Voide2WordsResult { // 结果是json格式的,下面的都是json结果中的标签值 private String sn; private String ls; private String bg; private String ed; private List ws; public static class Words { private String bg; private List cw; public static class Cw { private String w; private String sc; public String getW() { return w; } public void setW(String w) { this.w = w; } public String getSc() { return sc; } public void setSc(String sc) { this.sc = sc; } @Override public String toString() { return w; } } public String getBg() { return bg; } public void setBg(String bg) { this.bg = bg; } public List getCw() { return cw; } public void setCw(List cw) { this.cw = cw; } @Override public String toString() { String result = ""; for (Cw cwTmp : cw) { result += cwTmp.toString(); } return result; } } public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } public String getLs() { return ls; } public void setLs(String ls) { this.ls = ls; } public String getBg() { return bg; } public void setBg(String bg) { this.bg = bg; } public String getEd() { return ed; } public void setEd(String ed) { this.ed = ed; } public List getWs() { return ws; } public void setWs(List ws) { this.ws = ws; } @Override public String toString() { String result = ""; for (Words wsTmp : ws) { result += wsTmp.toString(); } return result; }}
7、对应的配置文件 <?xml version="1.0" encoding="utf-8"?>
8、copy自己的sdk和appid覆盖我的代码里面的,运行,安装后,一定要打开权限,demo没有做动态权限申请,录音等功能没有开启。 参考我的代码 github https://github.com/HungryGoogle/LeeVoice2WordDemo
备注:仅为个人开发者学习交流,如果转载或用于商业用途,后果自负。
请注明转载自 http://mp.blog.csdn.net/postedit/79120421
更多相关文章
- Android6.0系统增加那些新特性
- Android、web中的图片和语音的加密
- [置顶] android人脸识别——HowOld测测你的年龄和性别
- Android(安卓)Studio学写英语听写APP(音乐播放器)一
- Android(安卓)实现人脸识别检测时的扫描动画效果(二维码扫描动画
- android 端全新指纹识别框架,适配 android M / P, 可以识别到指
- Android(安卓)指纹识别,提升APP用户体验,从这里开始
- 融云 Android(安卓)sdk 2.1+ 稳定版 UI 和 模块功能自定义
- Unity与EasyAR