请把下面代码修改为微信小程序可用的typescript风格代码, import { useMessage } from 'naive-ui' import {…
- User
请把下面代码修改为微信小程序可用的typescript风格代码, import { useMessage } from 'naive-ui' import { computed, onBeforeUnmount, onMounted, ref } from 'vue' import { Base64 } from 'js-base64' import { TTSTypeEnum } from '../constants' import { t } from '@/locales' import website from '@/config/website' import { useUserStore } from '@/store'
const contentType = 'audio/mpeg' const splitMarker = 'SPLIT_MARKER'
export const useAudioPlay = (props?: { ttsConfig?: Chat.AppTTSConfigType }) => { const { ttsConfig } = props || {} const message = useMessage() // const useUserStore = useUserStore() const audioRef = ref(new Audio()) const audioLoading = ref(false) const audioPlaying = ref(false) const audioController = ref(new AbortController()) // const audio = new Audio() // audioRef.value = audio // Check whether the voice is supported const hasAudio = (() => { if (typeof window === 'undefined') return false if (ttsConfig?.type === TTSTypeEnum.none) return false if (ttsConfig?.type === TTSTypeEnum.model) return true const voices = window?.speechSynthesis?.getVoices?.() || [] // 获取语言包 const voice = voices.find((item) => { return item.lang === 'zh-CN' || item.lang === 'zh' }) return !!voice })()
const getAudioStream = async (input: string) => { if (!input) // eslint-disable-next-line prefer-promise-reject-errors return Promise.reject('Text is empty')
audioLoading.value = true audioController.value = new AbortController() const response = await fetch('/api/v1/audio/speech', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Blade-Requested-With': 'BladeHttpRequest', 'Authorization': `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`, [website.tokenHeader]: `bearer ${useUserStore().token}`, }, signal: audioController.value.signal, body: JSON.stringify({ ttsConfig, text: input.trim(), }), }).finally(() => { audioLoading.value = false }) if (!response.body || !response.ok) { const data = await response.json() message.error(t('chat.Audio Speech Error')) return Promise.reject(data) } return response.body}
// 播放web音频 const playWebAudio = (text: string) => { // window speech window?.speechSynthesis?.cancel() const msg = new SpeechSynthesisUtterance(text) const voices = window?.speechSynthesis?.getVoices?.() || [] // 获取语言包 const voice = voices.find((item) => { return item.lang === 'zh-CN' }) if (voice) { msg.onstart = () => { audioPlaying.value = true } msg.onend = () => { audioPlaying.value = false msg.onstart = null msg.onend = null } msg.voice = voice window.speechSynthesis?.speak(msg) } }
// 取消音频 const cancelAudio = () => { try { window.speechSynthesis?.cancel() audioController.value.abort('') } catch (error) {} if (audioRef.value) { audioRef.value.pause() audioRef.value.src = '' } audioPlaying.value = false }
/* Perform a voice playback */ const playAudioByText = async ({ text, buffer }: { text: string; buffer?: Uint8Array }) => { const playAudioBuffer = (buffer: Uint8Array) => { if (!audioRef.value) return const audioUrl = URL.createObjectURL(new Blob([buffer], { type: 'audio/mpeg' }))
- Assistant
- User
vue的ref, computed, onMounted, onUnmounted这些函数都需要修改成微信原生小程序,并且需要改用typescript语法,请重写上面程序
- Assistant
- User
上面程序请把其他方法写完整,不要省略
- Assistant