使用讯飞api 进行 文本转音频 和音频转文本

文本转音频

//appId, keySecret, host 为讯飞相关配置项  
try {
   
            String result = upload(url, appId, keySecret, host);
            String jsonStr = StringEscapeUtils.unescapeJavaScript(result);
            String orderId = String.valueOf(JSONUtil.getByPath(JSONUtil.parse(jsonStr), "content.orderId"));
            String convert = getResult(orderId, url);

            return convert;
        } catch (IOException | SignatureException | InterruptedException e) {
   
            e.printStackTrace();
            throw new CompletionException(e);
        }



 private String upload(String filePath, String appid, String keySecret, String host) throws IOException, SignatureException {
   
        HashMap<String, Object> map = new HashMap<>(16);
        HttpURLConnection urlConnection = FileReaderUtil.getUrlConnection(filePath);
        String fileName = FileUtils.getName(filePath);
        long fileSize = urlConnection.getContentLengthLong();

        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 = XunfeiHttpUtil.parseMapToPathParam(map);
        System.out.println("upload paramString:" + paramString);

        String url = host + "/v2/api/upload" + "?" + paramString;
        System.out.println("upload_url:" + url);
        String response = XunfeiHttpUtil.iflyrecUpload(url, urlConnection.getInputStream());

        return response;
    }   
    

  private String getResult(String orderId, String fileName) throws SignatureException, InterruptedException, IOException {
   
        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", 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值