简介:本地讲解的是 科大讯飞开发平台的语音转换功能的集成方法和封装
准备工作:
1、首先申请平台账号,创建我的应用,新增语音服务,获取Appid
2、下载创建的应用的对应SDK,这条很重要,每一个应用会对应一个SDK,用于做区分。
构建项目:
1、首先将需要的组件.jar包和.so文件引入工程,(注:由于CPU型号限制,speechDemo下的libs下的 arm64-v8a文件夹不要粘贴到我的工程,否则会报CPU型号异常而打不开APP,记得将你的其他组件的.so文件分别复制粘贴到 armeabi和armeabi-v7a文件下,否则会报错)
2、其次将下载的SDK的speechDemo中的 assets目录下的所有文件拷贝到我的项目中。注:必须是对应的SDK下的SpeechDemo中的assets文件夹,这个文件夹的内部文件其实也可以说是,区别于其他APP的SDK,内部包括录音Dialog的图片素材等文件
3、复制speechDemo中的相关Util组件到我的项目
下面贴几张图:("设置"界面我将它放在外部了,因为是SDK中提供好的Activity我就不贴代码了)
下面是我封装的speechDemo的方法:
package com.android.action.app.iflytek.util;
/**
* 录音转译功能封装类,用于语音识别和转换文字
*
* @version 1
* @param 2016.10.31 由Sherdon构建
* */
public class SpeechService {
private Context context;
private SpeechRecognizer speechRecognizer;//
private String mEngineType;// 转译类型
private static String TAG = "SpeechService_";
public SpeechService() {
};
/**
* 录音转译功能封装类
*
* @param mEngineType
* 转译模式类型((云端)在线,本地,混合)
* */
public SpeechService(Context context, String mEngineType) {
this.context = context;
this.mEngineType = mEngineType;
};
private RecognizerDialog speechRecognizerDialog;
private SharedPreferences mSharedPreferences;
/**
* 返回语音转换对象,
* @return SpeechRecognizer
* 返回语音识别器对象,在当前Activity的生命周期中需要用到,进行销毁暂停等操作
* */
public SpeechRecognizer initIflytek() {
// 注册
// SpeechUtility.createUtility(context,SpeechConstant.APPID +
// "=ab123456");这里替换为你自己的Appid
SpeechUtility.createUtility(context, SpeechConstant.APPID
+ StringConstants.VoiceAppid);
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
speechRecognizer = SpeechRecognizer.createRecognizer(context,
mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
speechRecognizerDialog = new RecognizerDialog(context, mInitListener);
mSharedPreferences = context.getSharedPreferences(
IatSettings.PREFER_NAME, Activity.MODE_PRIVATE);
// mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
// et_remark = ((EditText) findViewById(R.id.iat_text));
// mInstaller = new ApkInstaller(context);
// Activity activity = (Activity) context;
return speechRecognizer;
}
/**
* 初始化监听器。
*/
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG + "初始化监听器", "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
Toast.makeText(context, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT)
.show();
}
}
};
// 用HashMap存储听写结果
private Has