#因为该项目不适合图形展示,所以无图
需要使用录音功能,那么虚拟机是不行的,只能使用真机测试
集成到项目中,需要将讯飞SDK里的内容搬到项目对应位置
在AndroidMainfest.xml中添加相应权限,安卓多少版本需要在代码中动态获取权限,此配置无效。
在build.gradle中填加依赖
dependencies {
implementation files('libs\Msc.jar')
compile 'com.google.code.gson:gson:2.2.4'
}
在项目初始化时初始化讯飞工具。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
checkPermission();
//建议在创建时添加,xxxxx为对应的讯飞appId
SpeechUtility.createUtility(this, SpeechConstant.APPID + "=xxxxxx");
}
随意在页面上添加一个按钮,然后绑定监听事件
bnt_video =findViewById(R.id.bnt_video);
bnt_video.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
initSpeech();
}
});
public void initSpeech() {
// 语音配置对象初始化
// SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(NoteEdit.this, null);
//1.创建RecognizerDialog对象
RecognizerDialog mDialog = new RecognizerDialog(NoteEdit.this, null);
// 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
// mIat.setParameter(SpeechConstant.DOMAIN, "iat"); // domain:域名
// mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// mIat.setParameter(SpeechConstant.ACCENT, "mandarin"); // mandarin:普通话
//2.设置accent、language等参数
mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
//3.设置回调接口
mDialog.setListener(new RecognizerDialogListener() {
@Override
public void onResult(RecognizerResult recognizerResult, boolean isLast) {
if (!isLast) {
//解析语音
String result = parseVoice(recognizerResult.getResultString());
et_content.setText(result);
//获取焦点
et_content.requestFocus();
//将光标定位到文字最后,以便修改
et_content.setSelection(result.length());
}
}
@Override
public void onError(SpeechError speechError) {
}
});
//4.显示dialog,接收语音输入
mDialog.show();
}
public static String parseVoice(String resultString) {
Gson gson = new Gson();
Voice voiceBean = gson.fromJson(resultString, Voice.class);
StringBuffer sb = new StringBuffer();
ArrayList ws = voiceBean.ws;
for (Voice.WSBean wsBean : ws) {
String word = wsBean.cw.get(0).w;
sb.append(word);
}
return sb.toString();
}
public class Voice {
public ArrayList ws;
public class WSBean {
public ArrayList cw;
}
public class CWBean {
public String w;
}
}
技术难点:
android 64位机子兼容32位so包的操作
https://blog.csdn.net/qq_35534596/article/details/78010172
android讯飞语音开发常遇到的问题



