腾讯IM web版本实现迅飞语音听写(流式版)

本文基于TUIKit Demo项目集成迅飞语音听写(流式版)功能:

主要代码:

复制代码
// \src\TUIKit\components\TUIChat\message-input\index.vue
<template>
	<!-- 录音按钮 -->
	<div @touchstart.stop="touchstart" />
	<!-- 输入框 -->
	<MessageInputEditor  ref="editor"/>
</template>

<script setup lang="ts">
// xf-voice代码请参考本文附件
import { IatRecorder } from './xf-voice'

let xfVoice = null

const getPermission = () => {
return navigator.mediaDevices.getUserMedia({ audio: true })
}

const initXFVoice = (webSocketUrl) => {
    // 实例化迅飞语音听写(流式版)WebAPI
    let times = null
    xfVoice = new IatRecorder({
      APPID: '填写自己的APPID',
      webSocketUrl,
      onWillStatusChange: function (oldStatus, newStatus) {
      },
      onTextChange: function (text, sliceText) {
          //监听识别结果的变化
          if(sliceText) {
            xfVoiceWaiting.value = false
            editor.value.setEditorContent(sliceText)
          }
      }
    });
}

const touchstart = (event: any) => {
	// 获取网页录音授权
	getPermission().then(() => {
      xfVoice?.start()
    }, (error) => {
        console.log(`${error.name} : ${error.message}`);
    })
}

onMounted(() => {
 // getXFVoiceAuthUrl请求获取webSocket鉴权地址,接口需要后端提供
  getXFVoiceAuthUrl({}).then(res => {
    if(res?.data?.code == 200) {
      initXFVoice(res.data.data.url)
    } else {
      console.error('语音授权认证失败)
    }
  }).catch((e) => {
    console.error(e)
  })
})

</script>

对于uniapp 微信小程序版本的实现,后续文章会给出。

相关推荐
GIS之路7 分钟前
GeoTools 结合 OpenLayers 实现属性查询(二)
前端·信息可视化
烛阴15 分钟前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
AA-代码批发V哥19 分钟前
HTML之表单结构全解析
前端·html
聪聪的学习笔记30 分钟前
【1】确认安装 Node.js 和 npm版本号
前端·npm·node.js
小磊哥er42 分钟前
【前端工程化】你知道前端编码规范包含哪些内容吗
前端
菌菇汤1 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
Ramos丶1 小时前
【ABAP】 从无到有 新建一个Webdynpro程序
java·前端·javascript
Blossom.1181 小时前
机器学习在智能供应链中的应用:需求预测与物流优化
人工智能·深度学习·神经网络·机器学习·计算机视觉·机器人·语音识别
qq_411671981 小时前
vue3 的模板引用ref和$parent
前端·javascript·vue.js
清幽竹客3 小时前
vue-37(模拟依赖项进行隔离测试)
前端·vue.js