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

相关推荐
huashengzsj20 分钟前
Shopify个人建站如何搭建独立站:从零开始的完整指南
前端·网络·人工智能
程序猿小泓31 分钟前
从 Claude Code 学 Agent Harness:一个前端工程师的 AI Agent 学习笔记
前端·人工智能·学习
柒和远方1 小时前
从审计日志到可下载证据包:事务型 Outbox、租约 fencing 与保留水位
前端·后端·架构
minglie12 小时前
npm ali-oss库依赖冲突
前端·npm
CryptoPP2 小时前
BSE股票K线数据接入实战:从接口调用到前端图表展示
大数据·前端·网络·人工智能·websocket·网络协议
Random_index3 小时前
#Vue3篇: Vue 项目发版后如何提示用户刷新?基于 package.json 版本号的前端更新检测方案
前端·vue.js·json
没落英雄4 小时前
5. 从零开始搭建一个 AI Agent —— 人机协作与中断恢复
前端·人工智能·架构
品尚公益团队4 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
没有了遇见5 小时前
AI Agent 是什么?—— 一文理解 LLM、Memory、Skills、Tools、MCP、Workflow,Context
android·前端·程序员
后台开发者Ethan6 小时前
setState组件更新
前端·javascript·react