audio标签无法自动循环播放问题

10 篇文章 0 订阅

项目中有类似于美团外卖那种语音提示,如果有人预约,就会有消息提示,但是Chorme浏览器无法自动循环播放,之前在网上各种百度也没找到解决办法,最后经过一番努力,终于实现了这种效果。代码如下:

//这是audio标签  
             <audio
              src="../static/audios/yochima.mp3"//音频路径
              muted  //静音播放
              autoplay//自动播放
              loop//循环播放
              id="myPlay"
              style="display: none"
            ></audio>

<script>
    export default {
        data(){
            return {
                play: false,
                t1: 5001,
                t2: 6000,
                timer: null,
                reservationInfoNum :0,//预约消息数量
            }
        },
 mounted() {
    let audio = document.getElementById("myPlay");
    audio.muted = true; //关闭静音状态

    //getResevationInfoNum方法是我的接口
    getResevationInfoNum({ storeCode: this.storeCode }).then((res) => {
      if (res.code === 0) {
        console.log(res);
        this.reservationInfoNum = res.data;
        this.timer = setInterval(() => {
          getResevationInfoNum({ storeCode: this.storeCode }).then((res) => {
            if (res.code === 0) {
              this.reservationInfoNum = res.data;
              if (res.data > 0) {
                this.run();//语音播报代码
              }
            }
          });
        }, 5000);
      }
    });
  },
methods: {
    //语音播报
    run() {
      let audio = document.getElementById("myPlay");
      if (this.play) {
        return false;
      }
      audio.currentTime = 0; //播放音频的起始时间
      audio.volume = 1; //音频声音大小
      audio.muted = false; //关闭静音状态
      this.play = true;
      setTimeout(() => {
        this.play = false;
        audio.muted = true; //播放完毕,开启静音状态
      }, this.t2);
    },
},
    }
</script>

最终实现了,语音播报功能.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值