页面上多个audio只播放一个

// ts版-vue
private justPalyOne() {
    const audios = document.querySelectorAll("audio");
    // 暂停函数
    const pauseAll = (exclude: any): any => {
      audios.forEach((audio, index) => {
        if (exclude !== index) {
          audio.pause();
        }
      });
    };
    audios.forEach((audio, index) => {
      audio.addEventListener("play", ()=>{pauseAll(index)});
    });
}

 

// 普通js版
const justPalyOne = () => {
    const audios = document.querySelectorAll("audio");
    // 暂停函数
    const pauseAll = (exclude)=> {
      audios.forEach((audio, index) => {
        if (exclude !== index) {
          audio.pause();
        }
      });
    };
    audios.forEach((audio, index) => {
      audio.addEventListener("play", ()=>{pauseAll(index)});
    });
 }

 

posted @ 2021-04-27 10:43  丁少华  阅读(785)  评论(0编辑  收藏  举报