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