Vue使用speak-tts插件实现后台管理系统语音播报功能

本文介绍了一种在网页中实现语音播报的方法,通过引入`speak-tts`库,可以方便地进行文字转语音并控制播放、暂停和恢复。在模板中,通过点击图标触发不同的语音操作。在组件的`mounted`、`methods`和`destroyed`阶段分别初始化、控制语音和清理资源。关键词包括语音播报、暂停、恢复和取消。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

点击界面语音播报按钮,电脑进行语音播报

安装
npm install speak-tts
引用
import Speech from 'speak-tts'
使用
<template>
   <i class="el-icon-microphone" @click="play()" ></i>
   <i class="el-icon-video-pause" @click="paused()"></i> 
   <i class="el-icon-video-play" @click="goahead()" ></i>
</template>

<script>
import Speech from 'speak-tts'
data () {
    return {
      speech:null
    }
},
mounted(){
     this.SpeechInit()
},
methods:{
      SpeechInit(){
          this.speech = new Speech()  
          this.speech.setLanguage('zh-CN')
          this.speech.init().then(()=>{})
      },
      //播放按钮
      play(){
        this.speech.speak({
        text:"收款40万元",
        listeners: {
            //开始播放
            onstart: () => { console.log("Start utterance")},
            //判断播放是否完毕
            onend: () => { console.log("End utterance")},
            //恢复播放
            onresume: () => { console.log("Resume utterance") },
        },
        }).then(()=>{console.log("读取成功")})
    },
    //暂停
    paused() {
      this.speech.pause();
    },
    //从暂停处继续播放
    goahead() {
      this.speech.resume();
    },
},
//离开页面取消语音
 destroyed() {
   this.speech.cancel();
 },
</script>

方法
this.speech.pause();   //暂停
this.speech.cancel();  // 移除所有语音谈话队列中的谈话
this.speech.resume();  //从暂停处继续播放

转载文章
官方文档

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值