vue 播放音频


mounted() {

this.audio = new Audio();
    this.audio.src = "../../src/assets/alarm/Alarm.mp3";
    let playPromise;
    let second = 10;
   //加载音频
    playPromise = this.audio.play();
    if (playPromise) {
      playPromise
        .then(() => {
          // 音频加载成功
          that.tiemr = setInterval(() => {
            second--;
            console.log(this.audio.paused);
            if (this.audio.paused) {
              playPromise = this.audio.play();
            }
            if (second <= 0) {
              this.audio.pause();
              clearInterval(that.tiemr);
            }
          }, 1000);
        })
        .catch(e => {
          console.error("音频加载失败" + e);
        });
    }

}
 
// 重新加载
this.audio.load();
 
// 判断是否正在播放 返回布尔值
this.audio.paused
 
// 暂停
this.audio.pause();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM