共计 1285 个字符,预计需要花费 4 分钟才能阅读完成。
Vue 语音播报功能可以通过使用浏览器提供的 Web Speech API 来实现。这个 API 允许你的应用程序通过浏览器朗读文本,不用安装任何包和插件,直接调用。以下是一个简单的介绍,演示如何在 Vue 中使用语音提示功能:
一、JS 版本
Web Speech API
二、TS 版本
运用 TS 封装形式来实现语音播报功能。
创建一个 VoiceAnnouncements.ts 的文件,然后在应用的 Vue 页面进行引入该 ts 文件并使用。
class VoiceAnnouncements {
public synth = window.speechSynthesis // 启用文本
public msg: any = new SpeechSynthesisUtterance()
public language: string = 'zh-CN' // 使用的语言: 中文
public volume: number = 1 // 音量
public speed: number = 1 // 语速
public pitch: number = 1 // 音高
// 开始语音提示
startVoiceFunction = (content: String) => {
this.msg.text = content
this.msg.language = this.language
this.msg.volume = this.volume
this.msg.speed = this.speed
this.msg.pitch = this.pitch
this.synth.speak(this.msg)
}
// 停止语音提示
stopVoiceFunction = (content: any) => {
this.msg.text = content
this.msg. language = this. language
this.synth.cancel(this.msg)
}
}
// 传出实例,保证整个系统只存在单例的 Voice
const VoiceAnnouncementsInstance = new VoiceAnnouncements()
export default VoiceAnnouncementsInstance
原文地址: Vue 语音播报,不用安装任何包和插件,直接调用。
正文完