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

相关推荐
胡gh4 小时前
页面卡成PPT?重排重绘惹的祸!依旧性能优化
前端·javascript·面试
言兴5 小时前
# 深度解析 ECharts:从零到一构建企业级数据可视化看板
前端·javascript·面试
山有木兮木有枝_5 小时前
TailWind CSS
前端·css·postcss
烛阴6 小时前
TypeScript 的“读心术”:让类型在代码中“流动”起来
前端·javascript·typescript
杨荧6 小时前
基于Python的农作物病虫害防治网站 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python
Moment7 小时前
毕业一年了,分享一下我的四个开源项目!😊😊😊
前端·后端·开源
程序视点8 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端
silent_missile8 小时前
element-plus穿梭框transfer的调整
前端·javascript·vue.js
专注VB编程开发20年8 小时前
OpenXml、NPOI、EPPlus、Spire.Office组件对EXCEL ole对象附件的支持
前端·.net·excel·spire.office·npoi·openxml·spire.excel
古蓬莱掌管玉米的神8 小时前
coze娱乐ai换脸
前端