js原生方法实现语音播放【vue项目中】

可循环播放三次

复制代码
<button @click="play">语音播放</button>

data() {
    return {
        count:0;
    }
}

cyclePlay(){
       var time = new Date();
      console.log('time:',time)
      const asd = '请001号前来办理业务'
      const msg = new SpeechSynthesisUtterance()
      msg.text = asd
      let that = this
      msg.onend = function(){
        that.count+=1
        // alert('播放结束')
        setTimeout(function(){
      that.play()

        },1000)
      }
      window.speechSynthesis.speak(msg)
    },
    play(){
      console.log('this.count',this.count)
      if(this.count < 3){
        this.cyclePlay()
      }else{
        this.count = 0
        console.log('播放完毕')
      }
      // setInterval(this.cyclePlay(),1000)
    },    
复制代码

 

posted @   小那  阅读(486)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2022-03-15 vue移动端安卓软键盘弹出底部按钮顶起
点击右上角即可分享
微信分享提示