科大讯飞语音转写

JAVA SDK方式

https://www.xfyun.cn/doc/asr/voicedictation/Java-SDK.html

public String voiceToText(MultipartFile audioFile){
    result = new StringBuilder();
    try{
        if (recognizer == null){
            // 初始化识别对象
            recognizer = SpeechRecognizer.createRecognizer();
            recognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
            recognizer.setParameter( SpeechConstant.RESULT_TYPE, "plain" );
            // 前端点超时 开始录入音频后,音频前面部分最长静音时长。
            recognizer.setParameter(SpeechConstant.VAD_BOS,"5000");
            // 后端点超时要与运行SDK时配置的一样
            recognizer.setParameter(SpeechConstant.VAD_EOS,"10000");
        }
        recognizer.startListening(recognizerListener);
        byte[] buffer = audioFile.getBytes();
        if (buffer == null || buffer.length == 0) {
            System.out.println("no audio available!");
            recognizer.cancel();
        } else {
            int lenRead = buffer.length;
            recognizer.writeAudio( buffer, 0, lenRead );
            recognizer.stopListening();
            synchronized (lock) {
                lock.wait();//主线程等待
            }
            return result.toString();
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}
RecognizerListener recognizerListener = new RecognizerListener(){

    @Override
    public void onVolumeChanged(int i) {}

    @Override
    public void onBeginOfSpeech() {
        System.out.println("*************开始说话*************");
    }

    @Override
    public void onEndOfSpeech() {
        System.out.println("*************结束说话*************");
    }

    @Override
    public void onResult(RecognizerResult recognizerResult, boolean b) {
        System.out.println("*************返回结果*************");
        String text = recognizerResult.getResultString();
        System.out.println(text);
        result.append(text);
        if (b){
            synchronized (lock) {
                lock.notify();
            }
        }
    }

    @Override
    public void onError(SpeechError speechError) {
        if (null != speechError){
            System.out.println(speechError.getErrorDesc());
            synchronized (lock) {
                lock.notify();
            }
        }
    }

    @Override
    public void onEvent(int i, int i1, int i2, String s) {
        if(i == SpeechEvent.EVENT_SESSION_ID) {
            System.out.println("sid==" + s);
        }
    }
};

附上两个我遇到的问题解决办法
http://bbs.xfyun.cn/thread/comment?threadId=15132&commentId=63176
http://bbs.xfyun.cn/thread/9864

WebAPI 方式

https://www.xfyun.cn/doc/asr/ifasr_new/API.html

import cn.hutool.json.JSONUtil;
import cn.xfyun.sign.LfasrSignature;
import cn.xfyun.utils.HttpUtil;
import com.google.gson.Gson;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.*;
import java.net.URISyntaxException;
import java.security.SignatureException;
import java.util.HashMap;
puvlic class demo{
	private static final String HOST = "https://raasr.xfyun.cn";
    private static final String appid = "";
    private static final String keySecret = "";
	public static void main(String[] args) throws Exception {
	    String result = upload();
	    String jsonStr = StringEscapeUtils.unescapeJavaScript(result);
	    String orderId = String.valueOf(JSONUtil.getByPath(JSONUtil.parse(jsonStr), "content.orderId"));
	    String resp = getResult(orderId);
	}
	
	private static String upload() throws SignatureException, FileNotFoundException {
	    HashMap<String, Object> map = new HashMap<>(16);
	    File audio = new File(AUDIO_FILE_PATH);
	    String fileName = audio.getName();
	    long fileSize = audio.length();
	    map.put("appId", appid);
	    map.put("fileSize", fileSize);
	    map.put("fileName", fileName);
	    map.put("duration", "200");
	    LfasrSignature lfasrSignature = new LfasrSignature(appid, keySecret);
	    map.put("signa", lfasrSignature.getSigna());
	    map.put("ts", lfasrSignature.getTs());
	
	    String paramString = HttpUtil.parseMapToPathParam(map);
	    System.out.println("upload paramString:" + paramString);
	
	    String url = HOST + "/v2/api/upload" + "?" + paramString;
	    System.out.println("upload_url:"+ url);
	    String response = HttpUtil.iflyrecUpload(url, new FileInputStream(audio));
	
	    System.out.println("upload response:" + response);
	    return response;
	}
	
	private static String getResult(String orderId) throws SignatureException, InterruptedException {
	    HashMap<String, Object> map = new HashMap<>(16);
	    map.put("orderId", orderId);
	    LfasrSignature lfasrSignature = new LfasrSignature(appid, keySecret);
	    map.put("signa", lfasrSignature.getSigna());
	    map.put("ts", lfasrSignature.getTs());
	    map.put("appId", appid);
	    map.put("resultType", "transfer,predict");
	    String paramString = HttpUtil.parseMapToPathParam(map);
	    String url = HOST + "/v2/api/getResult" + "?" + paramString;
	    System.out.println("\nget_result_url:" + url);
	    while (true) {
	        String response = HttpUtil.iflyrecGet(url);
	        JsonParse jsonParse = gson.fromJson(response, JsonParse.class);
	        System.out.println("结果:" + response);
	        if (jsonParse.content.orderInfo.status == 4) {
	            System.out.println("订单完成:" + response);
	            return response;
	        } else if (jsonParse.content.orderInfo.status == -1) {
	            System.out.println("订单失败" + response);
	            break;
	        } else {
	            System.out.println("进行中...,状态为:" + jsonParse.content.orderInfo.status);
	            //建议使用回调的方式查询结果,查询接口有请求频率限制
	            Thread.sleep(7000);
	        }
	    }
	    return null;
	}
}

java SDK 方式转换的语音要求标准不能有断续,就是不能中间或者开头几秒没声。
webAPI的可以是大型会议录音那种转换。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值