js 利用Audio播放文件列表

1、主要利用Audio的监听事件addEventListener,监听到声音播放结束ended,播放下一个文件

audio.addEventListener('ended', function () {
        console.log('音频播放结束');
    });

2、示例:

复制代码
var voiceList = ['1.wav','2.wav','3.wav']
var voiceIndex= 0;
var audio = new Audio();
function  payNext(){
    if(voiceIndex >= voiceList.length){
        //audio.pause();   //暂停
        //voiceList = [];  //清空
        voiceIndex = 0;  //只保留这一行,可以循环播放
       }
    audio.src = voiceList[voiceIndex];
    audio.play();
    voiceIndex += 1;   //下一个音频
    audio.addEventListener('ended',payNextVoice,false);  //播放结束调自己
}
复制代码

 

posted @   ziff123  阅读(395)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示