当前位置: 首页 > news >正文

封装一个语言识别文字的方法

语音识别

需求:

  1. 参考官方文档,整合语音识别api
  2. callback 的写法改为 Promise 的版本
  • 在startRecord中:
  1. 参考文档实例化-开启转换
  2. 将录制的内容传递给录音识别
  3. 回调函数中的 Log,改为 Logger
  • 在closeRecord:
  1. 结束识别、释放资源
  2. 设置状态为 VoiceState.VOICEOVER

 

  async startRecord() {// 开始识别this.asrEngine = await speechRecognizer.createEngine({language: 'zh-CN',online: 1})// 保存组件的 this,后续通过_this来使用组件const _this = thisthis.asrEngine.setListener({onStart(sessionId: string, eventMessage: string) {console.info(`onStart, sessionId: ${sessionId} eventMessage: ${eventMessage}`);},onEvent(sessionId: string, eventCode: number, eventMessage: string) {console.info(`onEvent, sessionId: ${sessionId} eventCode: ${eventCode} eventMessage: ${eventMessage}`);},onResult(sessionId: string, result: speechRecognizer.SpeechRecognitionResult) {_this.keyword = result.result_this.onChange(result.result)console.info(`onResult, sessionId: ${sessionId} sessionId: ${JSON.stringify(result)}`);},onComplete(sessionId: string, eventMessage: string) {_this.onComplete(_this.keyword)_this.keyword = ''_this.voiceState = VoiceState.DEFAULTconsole.info(`onComplete, sessionId: ${sessionId} eventMessage: ${eventMessage}`);},onError(sessionId: string, errorCode: number, errorMessage: string) {console.error(`onError, sessionId: ${sessionId} errorCode: ${errorCode} errorMessage: ${errorMessage}`);}})const recognizerParams: speechRecognizer.StartParams = {sessionId: '10000',audioInfo: {audioType: 'pcm',sampleRate: 16000,soundChannel: 1,sampleBit: 16}}this.asrEngine?.startListening(recognizerParams)// 开始录音const audioStreamInfo: audio.AudioStreamInfo = {samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000,channels: audio.AudioChannel.CHANNEL_1,sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW}const audioCapturerInfo: audio.AudioCapturerInfo = {source: audio.SourceType.SOURCE_TYPE_MIC,capturerFlags: 0}const audioCapturerOptions: audio.AudioCapturerOptions = {streamInfo: audioStreamInfo,capturerInfo: audioCapturerInfo}this.audioCapturer = await audio.createAudioCapturer(audioCapturerOptions)this.audioCapturer.on('readData', (buffer) => {console.log('mk-logger', buffer.byteLength)this.asrEngine?.writeAudio('10000', new Uint8Array(buffer))})await this.audioCapturer.start()this.voiceState = VoiceState.VOICING}

http://www.lryc.cn/news/442191.html

相关文章:

  • 解决 iOS App Tracking Transparency 权限问题
  • ClickHouse 的底层架构和原理
  • rtmp推流
  • 【数据库】死锁排查方式
  • 去耦合的一些建议
  • SpringBoot+Thymeleaf图书管理系统
  • TDengine 签约前晨汽车,解锁智能出行的无限潜力
  • 模板字符串中定义方法并传参
  • Numpy 数组元素添加与元素删除函数详解
  • 【Python】高效图像处理库:pyvips
  • java项目之在线考试与学习交流网页平台源码(springboot)
  • 【Android源码】屏蔽系统通知出现在系统栏中
  • MySQL索引测试
  • 【软件设计】常用设计模式--观察者模式
  • 东北非国企就职体验
  • 经典sql题(二)求连续登录最多天数用户
  • A. Closest Point
  • 沟通更高效:微信群转移至企业微信操作攻略!
  • 计算机毕业设计 基于Python Django的旅游景点数据分析与推荐系统 Python+Django+Vue 前后端分离 附源码 讲解 文档
  • 关于安卓App自动化测试的一些想法
  • Bigemap GIS Office 2024注册机 全能版地图下载软件
  • 秦时明月6.2魔改版+GM工具+虚拟机一键端
  • firewalld实现NAT端口转发
  • 中国电子学会202309青少年软件编程(Python)等级考试试卷(二级)真题
  • 第四天旅游线路预览——从贾登峪到喀纳斯景区入口(贾登峪游客服务中心)
  • 个人常用命令
  • 如何根据协议请求去捕捉在个文件中发出去的
  • Lombok -----此java库 常用的注解及其功能总结
  • 纯前端表格导出Excel
  • sourceTree保姆级教程7:(合并某次提交)