虽然官网的代码copy下来没毛病,但是总是看着不爽,而且好多地方要使用,哈哈,请看代码
XFHelper初始化,sdk会通过appid初始化so库,初始化讯飞的服务,讲话时候,用MyApplication中的集合,将识别的字符存起来,当时别完成,会回调结果给实现XFHelper的类。
public class MyApplication extends Application {
private static Context context;
private static HashMap<String, String> mIatResults ;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
mIatResults = new LinkedHashMap<>();
}
public static Context getContext(){
return context;
}
public static HashMap<String, String> getmIatResults() {
return mIatResults;
}
public static void clearXFMap() {
mIatResults.clear();
}
}
单例,防止多次初始化,保证每次调用进行一次生命周期
public class XFHelper {
private static XFHelper xfHelper;
private static SpeechRecognizer recognizer;
private XFInterface xfInterface;
private XFHelper(XFInterface xfInterface) {
//进行retrofit的初始化
this.xfInterface = xfInterface;
init();
}
public void init() {
//改成自己的id,哈哈
SpeechUtility.createUtility(MyApplication.getContext(), SpeechConstant.APPID + "=59ae1823");
recognizer = SpeechRecognizer.createRecognizer(MyApplication.getContext(), i1 -> {
});
recognizer.setParameter(SpeechConstant.DOMAIN, "iat");
recognizer.setParameter(SpeechConstant.LANGUAGE, "en_us");
recognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
recognizer.setParameter(SpeechConstant.VAD_BOS, "2000");
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
recognizer.setParameter(SpeechConstant.VAD_EOS, "1000");
// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
recognizer.setParameter(SpeechConstant.ASR_PTT, "0");
recognizer.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
//保存音频文件到本地(有需要的话) 仅支持pcm和wav,且需要自行添加读写SD卡权限
recognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH, Constants.PATH);
}
public static XFHelper getRXFHelper(XFInterface xfInterface) {
if (xfHelper == null) {
synchronized (RetrofitHelper.class) {
if (xfHelper == null) {
xfHelper = new XFHelper(xfInterface);
}
}
}
return xfHelper;
}
public void startRecord() {
recognizer.startListening(mRecognizerListener);
}
private RecognizerListener mRecognizerListener = new RecognizerListener() {
@Override
public void onBeginOfSpeech() {
xfInterface.onBeginOfSpeech();
}
@Override
public void onError(SpeechError error) {
xfInterface.onError(error.getPlainDescription(true));
onEndOfSpeech();
}
@Override
public void onEndOfSpeech() {
xfInterface.onEndOfSpeech();
}
@Override
public void onResult(RecognizerResult results, boolean isLast) {
if (isLast) {
xfInterface.onResultSubmit();
} else {
printResult(results);
}
}
@Override
public void onVolumeChanged(int volume, byte[] data) {
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
}
};
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();
}
MyApplication.getmIatResults().put(sn, text);
}
}
大家发现了一个XFInterface的接口,他就是提供给外界类使用的,用来回调讯飞sdk服务给的识别的消息
public interface XFInterface {
//开始讲话
void onBeginOfSpeech();
//错误
void onError(String error);
//结束讲话
void onEndOfSpeech();
//识别结果
void onResultSubmit();
}
使用:(由于我们业务需求要上传录音,还需要sd卡权限,之前已申请)
这里简单写写,哈哈,大家根据实际情况
rxfHelper = XFHelper.getRXFHelper(this);
MyApplication.clearXFMap();
toRecord();
public void toRecord(){
if (ContextCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this.getActivity(), new String[]{
android.Manifest.permission.RECORD_AUDIO}, 1);
} else {
showGif(i);
rxfHelper.startRecord();
}
}
哈哈,大家别忘了导包和引入so库
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}