公司的项目要用到一个语音识别所以就弄了一下,国内的科大讯飞的名气比较响一些所以就选用了科大讯飞,废话不多说直接开干
1,下载sdk,注册获取APPid,这都不废话,需要什么就下载什么
2,把下载的东西放进工程中,重点1,注入依赖Msc.jar,Sunflower.jar.重点2,在Project标签下,在Main的目录下新建jniLibs目录,然后把下载到的所有so文件放在里面有好几个
3,进行编码
VoiceActivity
package io.dcloud.H5B79C397.activity;import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
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.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;import org.json.JSONException;
import org.json.JSONObject;import java.util.HashMap;
import java.util.LinkedHashMap;import io.dcloud.H5B79C397.R;public class VoiceActivity extends Activity implements View.OnClickListener {private SpeechSynthesizer mTts;private Button start, read;private Context mContext = this;// 默认发音人private String voicer = "xiaoyan";// 缓冲进度private int mPercentForBuffering = 0;// 播放进度private int mPercentForPlaying = 0;// 云端/本地单选按钮private RadioGroup mRadioGroup;// 语音听写对象private SpeechRecognizer mIat;// 语音听写UIprivate RecognizerDialog mIatDialog;private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();private EditText mResultText;private Toast mToast;private SharedPreferences mSharedPreferences;// 引擎类型private String mEngineType = SpeechConstant.TYPE_CLOUD;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_voice);SpeechUtility.createUtility(mContext, SpeechConstant.APPID +"=5982bde7");mIat = SpeechRecognizer.createRecognizer(VoiceActivity.this, mInitListener);mIatDialog = new RecognizerDialog(VoiceActivity.this, mInitListener);mSharedPreferences = getSharedPreferences("com.jredu.setting", Activity.MODE_PRIVATE);mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);mResultText = ((EditText) findViewById(R.id.et_content));mTts = SpeechSynthesizer.createSynthesizer(VoiceActivity.this, mTtsInitListener);mSharedPreferences = getSharedPreferences("com.jredu.setting", MODE_PRIVATE);mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);start = (Button) findViewById(R.id.bt_start);read = (Button) findViewById(R.id.bt_read);start.setOnClickListener(this);read.setOnClickListener(this);mEngineType = SpeechConstant.TYPE_CLOUD;//mInitListener//mTtsInitListener}int ret = 0; // 函数调用返回值@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_start:mResultText.setText(null);// 清空显示内容mIatResults.clear();// 设置参数setParam();boolean isShowDialog = mSharedPreferences.getBoolean("" , true);if (isShowDialog) {// 显示听写对话框mIatDialog.setListener(mRecognizerDialogListener);mIatDialog.show();showTip("倾听中");} else {// 不显示听写对话框ret = mIat.startListening(mRecognizerListener);if (ret != ErrorCode.SUCCESS) {showTip("听写失败,错误码:" + ret);} else {showTip("");}}break;case R.id.bt_read:String text = ((EditText) findViewById(R.id.et_content)).getText().toString();// 设置参数setParam();int code = mTts.startSpeaking(text, mTtsListener);// Log.d("======",""+code);
// /**
// * 只保存音频不进行播放接口,调用此接口请注释startSpeaking接口
// * text:要合成的文本,uri:需要保存的音频全路径,listener:回调接口
// */
// String path = Environment.getExternalStorageDirectory()+"/tts.pcm";
// int code = mTts.synthesizeToUri(text, path, mTtsListener);if (code != ErrorCode.SUCCESS) {if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {//未安装则跳转到提示安装页面} else {showTip("语音合成失败,错误码: " + code);}}break;default:break;}}private InitListener mTtsInitListener = new InitListener() {@Overridepublic void onInit(int code) {// Log.d(TAG, "InitListener init() code = " + code);if (code != ErrorCode.SUCCESS) {showTip("初始化失败,错误码:" + code);} else {// 初始化成功,之后可以调用startSpeaking方法// 注:有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成,// 正确的做法是将onCreate中的startSpeaking调用移至这里}}};private InitListener mInitListener = new InitListener() {@Overridepublic void onInit(int code) {// Log.d(TAG, "SpeechRecognizer init() code = " + code);if (code != ErrorCode.SUCCESS) {showTip("初始化失败,错误码:" + code);}}};private SynthesizerListener mTtsListener = new SynthesizerListener() {@Overridepublic void onSpeakBegin() {showTip("开始播放");}@Overridepublic void onSpeakPaused() {showTip("暂停播放");}@Overridepublic void onSpeakResumed() {showTip("继续播放");}@Overridepublic void onBufferProgress(int percent, int i1, int i2, String s) {mPercentForBuffering = percent;showTip(String.format("",mPercentForBuffering, mPercentForPlaying));}@Overridepublic void onSpeakProgress(int percent, int i1, int i2) {mPercentForPlaying = percent;showTip(String.format("朗读中",mPercentForBuffering, mPercentForPlaying));}@Overridepublic void onCompleted(SpeechError error) {if (error == null) {showTip("播放完成");} else if (error != null) {showTip(error.getPlainDescription(true));}}@Overridepublic void onEvent(int i, int i1, int i2, Bundle bundle) {}};private RecognizerListener mRecognizerListener = new RecognizerListener() {@Overridepublic void onBeginOfSpeech() {// 此回调表示:sdk内部录音机已经准备好了,用户可以开始语音输入showTip("开始说话");}@Overridepublic void onError(SpeechError error) {// Tips:// 错误码:10118(您没有说话),可能是录音机权限被禁,需要提示用户打开应用的录音权限。// 如果使用本地功能(语记)需要提示用户开启语记的录音权限。showTip(error.getPlainDescription(true));}@Overridepublic void onEndOfSpeech() {// 此回调表示:检测到了语音的尾端点,已经进入识别过程,不再接受语音输入showTip("结束说话");}@Overridepublic void onResult(RecognizerResult results, boolean isLast) {printResult(results);if (isLast) {// TODO 最后的结果}}@Overridepublic void onVolumeChanged(int volume, byte[] data) {showTip("当前正在说话,音量大小:" + volume);// Log.d(TAG, "返回音频数据:"+data.length);}@Overridepublic void onEvent(int eventType, int arg1, int arg2, Bundle obj) {// 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因// 若使用本地能力,会话id为null// if (SpeechEvent.EVENT_SESSION_ID == eventType) {// String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);// Log.d(TAG, "session id =" + sid);// }}};private void printResult(RecognizerResult results) {String text = JsonParser.parseIatResult(results.getResultString());String sn = null;// 读取json结果中的sn字段try {JSONObject resultJson = new JSONObject(results.getResultString());sn = resultJson.optString("sn");} catch (JSONException e) {e.printStackTrace();}mIatResults.put(sn, text);StringBuffer resultBuffer = new StringBuffer();for (String key : mIatResults.keySet()) {resultBuffer.append(mIatResults.get(key));}mResultText.setText(resultBuffer.toString());mResultText.setSelection(mResultText.length());}private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {public void onResult(RecognizerResult results, boolean isLast) {printResult(results);}/*** 识别回调错误.*/public void onError(SpeechError error) {showTip(error.getPlainDescription(true));}};private void showTip(String s) {mToast.setText(s);mToast.show();}private void setParam() {mIat.setParameter(SpeechConstant.PARAMS, null);// 设置听写引擎mIat.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);// 设置返回结果格式mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");String lag = mSharedPreferences.getString("iat_language_preference","mandarin");if (lag.equals("en_us")) {// 设置语言mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");} else {// 设置语言mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 设置语言区域mIat.setParameter(SpeechConstant.ACCENT, lag);}// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理mIat.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "5000"));// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音mIat.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "5000"));// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点mIat.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "1"));// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限// 注:AUDIO_FORMAT参数语记需要更新版本才能生效mIat.setParameter(SpeechConstant.AUDIO_FORMAT,"wav");mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/iat.wav");// 设置听写结果是否结果动态修正,为“1”则在听写过程中动态递增地返回结果,否则只在听写结束之后返回最终结果// 注:该参数暂时只对在线听写有效mIat.setParameter(SpeechConstant.ASR_DWA, mSharedPreferences.getString("iat_dwa_preference", "0"));}@Overrideprotected void onDestroy() {super.onDestroy();// 退出时释放连接mIat.cancel();mIat.destroy();}@Overrideprotected void onResume() {// 开放统计 移动数据统计分析//FlowerCollector.onResume(IatDemo.this);//FlowerCollector.onPageStart(TAG);super.onResume();}@Overrideprotected void onPause() {// 开放统计 移动数据统计分析/// FlowerCollector.onPageEnd(TAG);//FlowerCollector.onPause(IatDemo.this);super.onPause();}
}
activity_voice.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="io.dcloud.H5B79C397.activity.VoiceActivity"><EditText
android:id="@+id/et_content"android:layout_marginTop="10dp"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:layout_width="match_parent"android:gravity="top"android:layout_height="180dp"/><Button
android:id="@+id/bt_start"android:layout_marginTop="10dp"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="倾听"/><Button
android:id="@+id/bt_read"android:layout_marginTop="10dp"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="读取"/></LinearLayout>
JsonParser
package io.dcloud.H5B79C397.activity;import android.text.TextUtils;import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;/*** Created by Administrator on 2017/8/3 0003.*/public class JsonParser {public static String parseIatResult(String json) {if(TextUtils.isEmpty(json))return "";StringBuffer ret = new StringBuffer();try {JSONTokener tokener = new JSONTokener(json);JSONObject joResult = new JSONObject(tokener);JSONArray words = joResult.getJSONArray("ws");for (int i = 0; i < words.length(); i++) {JSONArray items = words.getJSONObject(i).getJSONArray("cw");// 听写结果词,默认使用第一个结果JSONObject obj = items.getJSONObject(0);ret.append(obj.getString("w"));}} catch (Exception e) {e.printStackTrace();}return ret.toString();}}
上几张图