下载官方Java SDK,获取到核心jar包
新建工程,将核心jar包导入项目工程中
将SDK中文件同样复制到项目工程中
核心测试代码:
package com.iflytek;import com.iflytek.cloud.speech.*;
import com.iflytek.util.JsonParser;/*** @program:Test* @description:TODO* @author:zhaoyanning* @create:2022/6/22 16:06**/
public class Test {/*** 语音听写对象*/SpeechRecognizer speechRecognize;public Test() {// 初始化听写对象speechRecognize = SpeechRecognizer.createRecognizer();}/*** 开始监听并向讯飞服务器发送语音* @return*/public int startListen() {if (!speechRecognize.isListening()){speechRecognize.startListening(mRecoListener);} else{speechRecognize.stopListening();}return 0;}/*** 监听器*/private RecognizerListener mRecoListener = new RecognizerListener(){//获取结果@Overridepublic void onResult(RecognizerResult results, boolean isLast){//用json来获取结果String text = results.getResultString();String newTest = JsonParser.parseIatResult(text);System.out.print(newTest);}//会话发生错误回调接口@Overridepublic void onError(SpeechError error) {//error.getPlainDescription(true); //获取错误码描述}//开始录音@Overridepublic void onBeginOfSpeech() {}//音量值0~30@Overridepublic void onVolumeChanged(int volume){}//结束录音@Overridepublic void onEndOfSpeech() {}//扩展用接口@Overridepublic void onEvent(int eventType,int arg1,int arg2,String msg) {}};public static void main(String[] args) {//初始化听写对象Test t=new Test();StringBuffer param=new StringBuffer();param = new StringBuffer();param.append("appid=xxxx");SpeechUtility.createUtility(param.toString());t.startListen();}
}