Java 接入讯飞语音听写Speech to Text(STT)功能

根据官方提供的 WebIATWS 工具扩展修改,接入了讯飞的语音听写(STT)服务

讯飞认证配置

public class XFAuthorityConfig {
   
    public static final String hostUrl = "https://iat-api.xfyun.cn/v2/iat";
    public static final String apiKey = "xxxx";
    public static final String apiSecret = "xxx";
    public static final String appid = "5ede17d7";
}

封装监听器

public class WrapListener extends WebSocketListener {
   
    private static final Logger LOGGER = LoggerFactory.getLogger(WrapListener.class);
    // 下面三个参数是我根据需要新增的
    // file 是要听写的音频文件,而language 支持 zh-CN,en-US]
    // result 是返回结果
    private InputStream file;
    private String language;
    private String result;

    private String appId = XFConfig.appid;
    public static final int StatusFirstFrame = 0;
    public static final int StatusContinueFrame = 1;
    public static final int StatusLastFrame = 2;
    public static final Gson json = new Gson();
    Decoder decoder = new Decoder();
    // 开始时间
    private static Date dateBegin = new Date();
    // 结束时间
    private static Date dateEnd = new Date();
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");

    public InputStream getFile() {
   
        return file;
    }

    public void setFile(InputStream file) {
   
        this.file = file;
    }

    public String getLanguage() {
   
        return language;
    }

    public void setLanguage(String language) {
   
        this.language = language;
    }

    public String getResult() {
   
        return result;
    }

    public void setResult(String result) {
   
        this.result = result;
    }

    @Override
    public void onOpen(WebSocket webSocket, Response response) {
   
        super.onOpen(webSocket, response);
        int frameSize = 1280;
        int intervel = 40;
        int status = 0;
        try {
   
            byte[] buffer = new byte[frameSize];
            // 发送音频
            end:
            while (true) {
   
                int len = file.read(buffer);
                if (len == -1) {
   
                    status = StatusLastFrame;
                }
                switch (status) {
   
                    case StatusFirstFrame:
                        JsonObject frame = new JsonObject();
                        JsonObject business = new JsonObject()
                        JsonObject common = new JsonObject();
                        JsonObject data = new JsonObject();
                        // 填充common
                        common.addProperty("app_id", appId);
                        //填充business
                        business.addProperty("language", language);
                        business.addProperty("domain", "iat");
                        business.addProperty("accent", "mandarin");//中文方言请在控制台添加试用,添加后即展示相应参数值
                        
                        business.addProperty("dwa", "wpgs");
                        data.addProperty("status", StatusFirstFrame);
                        data.addProperty("format", "audio/L16;rate=8000");
                        data.addProperty("encoding", "raw");
                        data.addProperty("audio", Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
                        //填充frame
                        frame.add("common", common);
                        frame.add("business", business);
                        frame.add(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值