腾讯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 微信小程序版本的实现,后续文章会给出。

相关推荐
修己xj1 小时前
打造专属博文封面神器:一个开源免费的博文封面生成器ThisCover
前端
kyriewen1 小时前
面试8家前端岗位后,我发现了一个残酷的事实:AI不是加分项,是门槛
前端·javascript·面试
Fighting_p1 小时前
【面试 - el-select问题及解决】wujie 微前端下子系统 el-select 多选 filterable 过滤失效
前端
吃口巧乐兹1 小时前
AI 全栈时代,为什么要服务端使用 NestJs
前端
yingyima1 小时前
Redis 延迟任务队列:凌晨3点服务器报警的救星
前端
weiggle1 小时前
第三篇:可组合函数(Composable)——Compose 的基石
android·前端
前端环境观察室1 小时前
别只看 task success:AI Agent 浏览器自动化真正要补的是环境证据链
前端·后端
huakoh1 小时前
LangChain 实战:用混合检索啃下 1000 页 PDF,搭一个长文档问答 Agent
前端
Dazer0071 小时前
Edge 浏览器绕过 HTTPS 证书错误
前端·https·edge
元让_vincent2 小时前
Spark 2.0:面向 Web 的 3DGS 可视化与大场景渲染平台详解
前端·3d·spark·渲染·轻量化·3dgs·lod