HKUST Xunfei Speech Recognition Test

Download the official Java SDK and get the core jar package

insert image description here

Create a new project and import the core jar package into the project project

Copy the files in the SDK to the project project as well

insert image description here

Core test code:

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(){
        //获取结果
        @Override
        public void onResult(RecognizerResult results, boolean isLast){
            //用json来获取结果
            String text = results.getResultString();
            String newTest = JsonParser.parseIatResult(text);
            System.out.print(newTest);
        }

        //会话发生错误回调接口
        @Override
        public void onError(SpeechError error) {
            //error.getPlainDescription(true); //获取错误码描述
        }
        //开始录音
        @Override
        public void onBeginOfSpeech() {}
        //音量值0~30
        @Override
        public void onVolumeChanged(int volume){}
        //结束录音
        @Override
        public void onEndOfSpeech() {}
        //扩展用接口
        @Override
        public 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();
    }
}

Guess you like

Origin blog.csdn.net/weixin_44455388/article/details/125414239