/*** 指定录音文件转文字(异步)* @param bo* @return*/@Overridepublic JSONObject recognizeAudio(QueryRecognizeBo bo){Map<String,Object> recordMap = extLxccCdrMapper.queryRelativePath(bo.getCallId());String filePath = (String) recordMap.get(relativePathKey);if (CollectionUtils.isEmpty(recordMap) || StringUtils.isBlank(filePath)|| recordMap.get(fileSizeKey) == null || (Integer) recordMap.get("fileSize") == 0){return ResponseUtil.setResponseMsg("000005","录音不存在");}List<RecognizeText> list = recognizeMapper.selectRecognizeTxt(bo.getCallId());log.info("recognizeAudio list :{}",list);if (CollectionUtils.isEmpty(list)){recognizeDelayQueue.doProcess(filePath, new RecognizeDelayQueue.ItemQueueTask() {@Overridepublic ItemDelayed setItemQueue(String taskId, LfasrClient lfasrClient) {return setItemDelayed(taskId,bo.getCallId(),bo.getCallBackUrl(),lfasrClient);}});return ResponseUtil.setResponseMsg(SUCCESS_CODE,SUCCESS_WAIT);} else {return setResponse(SUCCESS_CODE,SUCCESS_MSG,bo.getCallId(),list);}}public ItemDelayed setItemDelayed(String taskId,String callbackUrl,String callId,LfasrClient lfasrClient){return new ItemDelayed(taskId,lfasrClient,callId,callbackUrl,(int) LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")),(int) LocalDateTime.now().plusMinutes(10).toEpochSecond(ZoneOffset.of("+8")));}public JSONObject setResponse(String code, String msg, String callId, List<RecognizeText> list){JSONArray array = JSONArray.parseArray(list.toString());JSONObject data = new JSONObject();data.put("callId",callId);data.put("recognizeData",array);JSONObject jsonObject = new JSONObject();jsonObject.put("code",code);jsonObject.put("message",msg);jsonObject.put("data",data);return jsonObject;}

队列处理

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.msp.lfasr.LfasrClient;
import com.iflytek.msp.lfasr.model.Message;
import com.lxccb.common.util.http.HttpDeal;
import com.lxccb.mybatis.bean.auto.LxccRecognizeRequest;
import com.lxccb.mybatis.bean.auto.LxccRecognizeTxt;
import com.lxccb.mybatis.mapper.auto.LxccRecognizeRequestMapper;
import com.lxccb.mybatis.mapper.auto.LxccRecognizeTxtMapper;
import com.lxccb.mybatis.mapper.ext.ExtLxccRecognizeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.ref.SoftReference;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.DelayQueue;/*** @author: datszhang* @Date: 2021/5/29 15:20* @Description: redis hash 延时队列*/
@Slf4j
@Component
public class RecognizeDelayQueue {/*** 识别状态*/private final Integer NINE = 9;/*** 间隔10秒*/private final Integer interval = 10;private static final String APP_ID = "2aaf4d90";private static final String SECRET_KEY = "cdec42ef6bb2018d755a33a211fe30a4";@Autowiredprivate LxccRecognizeRequestMapper lxccRecognizeRequestMapper;@Autowiredprivate LxccRecognizeTxtMapper lxccRecognizeTxtMapper;@Autowiredprivate ExtLxccRecognizeMapper lxccRecognizeMapper;private final String SUCCESS_CODE = "000000";private final String SUCCESS_MSG = "成功";private final String SUCCESS_WAIT = "识别中";@Autowiredprivate ExtLxccRecognizeMapper recognizeMapper;public DelayQueue<ItemDelayed> delayQueue = new DelayQueue<>();public DelayQueue<ItemDelayed> putDelayQueue(ItemDelayed item){this.delayQueue.offer(item);return this.delayQueue;}/*** 识别处理* @param filePath 存储路径* @param itemQueueTask*/public void doProcess(String filePath,RecognizeDelayQueue.ItemQueueTask itemQueueTask)  {ItemDelayed item = addItemToQueue(filePath,itemQueueTask);Integer count = recognizeMapper.selectCallIdCount(item.getCallId());Long recognizedId = null;if (count == null || count <= 0 ){recognizedId = insertDb(item.getCallId(),item.getCallbackUrl());newThreadRun(recognizedId);}log.info("doProcess -> count = {} ,recognizedId :{} end.",count,recognizedId);}public void newThreadRun(Long recognizedId){log.info("newThreadRun -> start.  delayQueue.size = {}",delayQueue.size());new Thread(new Runnable(){@Overridepublic void run() {while (true){try {ItemDelayed itemDelayed = delayQueue.take();log.info("newThreadRun -> itemDelayed:{}",JSONObject.toJSONString(itemDelayed));Message message = itemDelayed.getLfasrClient().getProgress(itemDelayed.getTaskId());// {"data":"{\"status\":2,\"desc\":\"音频合并完成\"}","errNo":0,"ok":0}log.info("newThreadRun -> message:{}",JSONObject.toJSONString(message));JSONObject object = JSON.parseObject(message.getData());Integer status = object.getInteger("status");if (status.equals(NINE)){Message result = itemDelayed.getLfasrClient().getResult(itemDelayed.getTaskId());log.info("newThreadRun -> callId:{},result:{}",itemDelayed.getCallId(),JSONObject.toJSONString(result));if (result == null || result.getOk() == -1){log.error("newThreadRun failed reason:{}",result.getFailed());} else {parseMessage(itemDelayed.getCallId(),itemDelayed.getCallbackUrl(),result,recognizedId);}break;} else {//没有结果,且还需要查询log.info("newThreadRun -> callId:{} ,status :{}  is not 9.",status,itemDelayed.getCallId());itemDelayed.setTime(nextDelay(interval));putDelayQueue(itemDelayed);}} catch (InterruptedException e) {log.error("RedisHashDelayQueue.newThreadRun error :",e);}}}}).start();}/*** 下一次触发时间* @param plusTime* @return*/public int nextDelay(Integer plusTime){return (int) LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + plusTime;}public ItemDelayed addItemToQueue(String filePath,RecognizeDelayQueue.ItemQueueTask itemQueueTask){LfasrClient lfasrClient = LfasrClient.getInstance(APP_ID, SECRET_KEY);SoftReference<LfasrClient> sflc = new SoftReference<>(lfasrClient);Message message = lfasrClientUpload(filePath,sflc.get());log.info("addItemToQueue -> message:{}",JSONObject.toJSONString(message));SoftReference<Message> sfm = new SoftReference<>(message);ItemDelayed item = itemQueueTask.setItemQueue(sfm.get().getData(),lfasrClient);this.putDelayQueue(item);return item;}public Message lfasrClientUpload(String filePath,LfasrClient lfasrClient){//2、上传//2.1、设置业务参数Map<String, String> param = new HashMap<>(16);//是否开启分词:默认 false//param.put("has_participle","true");//转写结果中最大的候选词个数:默认:0,最大不超过5//param.put("max_alternatives","2");//是否开启角色分离:默认为falseparam.put("has_seperate","true");//发音人个数,可选值:0-10,0表示盲分:默认 2param.put("speaker_number","2");//角色分离类型 1-通用角色分离;2-电话信道角色分离:默认 1//param.put("role_type","1");//语种: cn-中文(默认);en-英文(英文不支持热词)param.put("language", "cn");//垂直领域个性化:法院-court;教育-edu;金融-finance;医疗-medical;科技-tech//param.put("pd","finance");Message task = lfasrClient.upload(filePath, param);log.info("lfasrClientUpload -> 转写任务 task:{}", JSONObject.toJSONString(task));return task;}public boolean removeItem(ItemDelayed item){return delayQueue.remove(item);}private void parseMessage(String callId,String callbackUrl,Message result,Long recognizedId){JSONArray data = JSONObject.parseArray(result.getData());List<LxccRecognizeTxt> list = new ArrayList<>(data.size());JSONArray ja = new JSONArray(data.size());for (int i = 0; i < data.size(); i++){JSONObject jj = data.getJSONObject(i);list.add(setText(jj,recognizedId));ja.add(setCallback(jj));}lxccRecognizeMapper.batchInsertText(list);JSONObject callbackData = new JSONObject();callbackData.put("callId",callId);callbackData.put("recognizeData",ja);JSONObject callback = new JSONObject();callback.put("data",callbackData);callback.put("message",SUCCESS_MSG);callback.put("code",SUCCESS_CODE);sendCallback(callbackUrl,callbackData);}private JSONObject setCallback(JSONObject jd){JSONObject jj = new JSONObject();jj.put("bg",jd.getInteger("bg"));jj.put("ed",jd.getInteger("ed"));jj.put("onebest",jd.getString("onebest"));jj.put("speaker",jd.getInteger("speaker"));return jj;}private LxccRecognizeTxt setText(JSONObject jd,Long recognized){LxccRecognizeTxt entity = new LxccRecognizeTxt();entity.setBeginTime(jd.getInteger("bg"));entity.setEndTime(jd.getInteger("ed"));entity.setOnebest(jd.getString("onebest"));entity.setRecognizeId(recognized);entity.setSpeaker(jd.getInteger("speaker"));return entity;}private Long insertDb(String callId,String callbackUrl){LxccRecognizeRequest entity = new LxccRecognizeRequest();entity.setCallBackUrl(callbackUrl);entity.setCallId(callId);entity.setCreateTime(new Date());lxccRecognizeRequestMapper.insertSelective(entity);return entity.getId();}private void sendCallback(String callbackUrl,JSONObject callbackData){log.info("sendCallback -> callbackUrl:{},callbackData:{}",callbackUrl,callbackData);HttpDeal.post(callbackUrl,callbackData.toJSONString());}public interface ItemQueueTask{/*** 添加任务ID到延时队列数据* @param taskId*/ItemDelayed setItemQueue(String taskId,LfasrClient lfasrClient);}
}

延时对象

import com.iflytek.msp.lfasr.LfasrClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;/*** @Auther: datszhang* @Date: 2019/11/9 14:44* @Description: 只对redis的hash有用*/
@Slf4j
@Data
public class ItemDelayed implements Delayed {/*** 任务ID*/private String taskId;/*** 客户端实例*/private LfasrClient lfasrClient;private String callId;/*** 用户的回调地址*/private String callbackUrl;/*** 延时时间*/private Integer time;/*** 截止时间*/private Integer deadline;public ItemDelayed(){}public ItemDelayed(String taskId,LfasrClient lfasrClient,String callId,String callbackUrl,Integer time,Integer deadline) {this.taskId = taskId;this.lfasrClient = lfasrClient;this.callId = callId;this.callbackUrl = callbackUrl;this.time = time;this.deadline = deadline;}/*** delayQueue.take()中的getDelay 获取剩余激活时间* @param unit* @return*/@Overridepublic long getDelay(TimeUnit unit) {if (this.time >= this.deadline){return -1L;}long delayTime = this.time - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));return delayTime;}@Overridepublic int compareTo(Delayed o) {ItemDelayed item = (ItemDelayed) o;long diff = this.time - item.time;if (diff <= 0) {return -1;}else {return 1;}}
}

科大讯飞指定录音文件转文字(异步)相关推荐

  1. SwiftUI 语音合成与语言识别教程之 03 实现录音文件转文字(含完整项目源码)SFSpeechURLRecognitionRequest

    前期知识回顾 <SwiftUI 语音合成与语言识别教程之 01 Speech框架简介>我们介绍了Speech框架是什么,知道了可以使用Speech进行多语言识别. <SwiftUI ...

  2. 录音文件转文字,有了这个工具,再也不用担心记不上笔记了

    对于文字工作者及办公人员来说,录音及语音文件的整理既必要又繁琐.录音一小时整理需要大半天,消耗人力时间,而且效率低.大部分采用手机录音或录音笔录音的,等待整理录音的过程是很难受的,一段录音可能要反复听 ...

  3. 开会录音转文字什么软件好?分享一个录音文件转文字的方法

    大家现在的会议记录方式还在手写笔记吗?其实我们可以使用先录音再转文字的方法来节约时间.那么,有没有好用的开会录音转文字软件呢?不用着急,接下来将介绍一个非常便捷的转换方法,可以快速将语音文件转换成文字 ...

  4. 如何恢复录音删除的录音文件_手机通话录音后!点击这个按钮,就能将录音文件一键转为文字...

    通话录音是很多朋友都会用的功能,比如接听重要电话,或者和领导电话会议都会用到这个功能. 但是将录音中的内容整理出来就比较麻烦了,不仅要一遍遍听,还要一边手写记录,其实点击这个按钮,就能将录音中的内容一 ...

  5. 怎样将手机中的录音转换成文字

    像记者.新闻采访者.很多时候都会把访问的内容先录下来,然后结束后在慢慢整理,以前很多人是用录音笔记录,然后在一遍一遍的听,这个解决问题的一种办法,但是现在随着科技的发展,已经有很多的录音工具可以直接将 ...

  6. 录音文件转换成文字的方法

    可以把录音转文字的方法有很多种,根据自己不同的需求选择不同的转换工具以及转换的方法,下面小编为大家分享几个转换的方法,方法简单,满足你的转换需求! 借助的工具: 1:迅捷文字转语音软件 2:语音云服务 ...

  7. c++ 实现录音并且指定到文件_通话自动录音,留下美好回忆,记录完整录音证据...

    手机通话,如果自动录音多好,许多人与我一样抱有这个想法. 记得华为Android版本5.0时代,手机没有自动录音功能,我一直到网上下载自动通话录音软件,有时甚至是下载ROOT版的带自动通话功能的EMU ...

  8. c++ 实现录音并且指定到文件_搜狗发布四款AI录音笔,4大核心功能开启AI录音新时代...

    一直以来,录音笔行业都被认为是一个"单纯"的行业--纵观市面上的大部分录音笔,都仅仅是在满足大部分用户录音的需求.诚然,录音笔的用途决定了"录音"一定是其主旋律 ...

  9. c++ 实现录音并且指定到文件_2020年的办公装备新选择,搜狗AI录音笔E1深度评测...

    作为国内AI录音笔产品的"领军人物",搜狗于去年推出的AI录音笔C1系列一经上市就广受用户好评,成为了十足的"爆款".或许是为了满足不同需求的用户,搜狗在202 ...

  10. 怎么将录音文件转换成文字呢?

    录音是小伙伴们在日常生活和工作中经常使用的一种操作.毕竟每个手机基本都有录音功能.这个功能让录音变得非常简单,每一个小伙伴职场中,参加各种大大小小的会议是无法回避的任务.而在会议过程中,打开电话录音, ...

最新文章

  1. class转java_[拒绝套路,纯干货]这一百多道 Java 基础问题你掌握了吗?
  2. 使用工具类时尽量使用私有的无参构造函数
  3. 编程沉思-做一款小巧而好用的截图软件
  4. linux修改path路径
  5. 《那些年啊,那些事——一个程序员的奋斗史》——63
  6. 用Java打开一个网页
  7. 深度学习系列--1.入坑模型: 线性回归,logistic 回归,softmax分类器
  8. 【react】---函数化编程的理解,柯里化函数及返柯里化函数的理解...
  9. Android的NDK开发(2)————利用Android NDK编写一个简单的HelloWorld
  10. 三、地址族与数据序列
  11. 疫情严峻,被迫在家办公
  12. 汽车汽配行业DMS渠道商系统精准掌握渠道库存,提升市场响应能力
  13. Java三种方法实现字符串排序
  14. 舞台音效控制软件_舞台音乐控制软件下载
  15. 模块三 day22 并发编程(上)
  16. 解决Error response from daemon: conflict: unable to delete bf6a13bd36ca (must be forced)
  17. 游戏性能优化的五个方向
  18. java float 判断整数_判断一个数是否是整数
  19. jQuery实现AJAX定时刷新局部页面实例
  20. Excel2010中安装MegaStat插件 MegaStat for Excel2010(2007也适用)

热门文章

  1. 富途量化交易接口使用什么语言编程?
  2. 前端学习路线(个人愚见)
  3. 微信小程序|开发FAQ篇
  4. 专项训练——判断推理
  5. python无法打开_python程序无法打开是怎么回事
  6. windows系统:Xshell下载安装+连接服务器
  7. VS2015编辑图片
  8. 云计算机短网址,最新官方新浪短网址生成API接口与在线短网址缩短工具分享
  9. 关于环信客服的集成与使用
  10. SVG (SVG的概念 、SVG 实例 、SVG 在HTML中 、SVG 矩形 、SVG 圆形 、SVG 椭圆 、SVG 直线 、SVG 多边形、svg验证码 )