【问题标题】:How to loop through multiple audio files and play them sequentially using javascript?如何循环播放多个音频文件并使用javascript按顺序播放?
【发布时间】:2016-09-14 15:09:06
【问题描述】:

单击按钮我想播放 4 个音频,三个来自兄弟 ID 的音频,最后一个来自单击的 ID。请帮我解决问题。

我已更改代码,但音频仍未按顺序播放。最后一个音频首先播放,然后是 2nd 和 3rd,音频之间没有足够的间隔。

$(document).on('click', '.phonics_tab .audioSmImg_learn', function () {
    var PID = '#' + $(this).parents('.phonics_tab').attr('id');
    audioFileName = 'audio/' + $(this).attr('id') + '.mp3';
    var audioElmnt = {};
    var audioFileName1 = {};

    $(PID + ' .word_box_item').each(function (inx, i) {
        if (inx >= 1) {
            audioElmnt[inx - 1].pause();
            audioElmnt[inx - 1].currentTime = 0;
        }
        audioElmnt[inx] = document.createElement('audio');
        audioElmnt[inx].setAttribute('autoplay', 'false');
        audioFileName1[inx] = 'audio/' + $(this).children('h2').attr('id') + '.mp3';
        audioElmnt[inx].setAttribute('src', audioFileName1[inx]);
        audioElmnt[inx].load();

         //in previous code your inx only run for the last item.
        playAudio(audioElmnt[inx], 300); // here the object will be sent to the function and will be used inside the timer.
    });

    function playAudio(audioElmnt, delay){
        setTimeout(function () {
            audioElmnt.play();
        }, delay);
    }   

    setTimeout(function () {
        audioElement.currentTime = 0;
        audioElement.pause();
        audioElement.setAttribute('src', audioFileName);
        audioElement.load();
        audioElement.play();
    }, 500);
});

【问题讨论】:

    标签: javascript jquery html audio


    【解决方案1】:

    您不应该在循环中使用 setTimeout ,它的作用与所有其他编程语言不同。你的解决方案应该是这样的。 在之前的代码中,您的变量 inx 仅针对最后一项运行。

    $(document).on('click', ' .audioImg', function () {
        var ParentID = '#' + $(this).parents('.parent').attr('id');
        audioFileName = 'audio/' + $(this).attr('id') + '.mp3';
        var audioElmnt = {};
        var audioFileName1 = {};
    
        $(PID + ' .item').each(function (inx, i) {
            if (inx >= 1) {
                audioElmnt[inx - 1].pause();
                audioElmnt[inx - 1].currentTime = 0;
            }
    
            audioElmnt[inx] = document.createElement('audio');
            audioElmnt[inx].setAttribute('autoplay', 'false');
            audioFileName1[inx] = 'audio/' + $(this).children('h2').attr('id') + '.mp3';
            audioElmnt[inx].setAttribute('src', audioFileName1[inx]);
    
            audioElmnt[inx].load();
    
           //in previous code your inx only run for the last item.
            playAudio(audioElmnt[inx], 150); // here the object will be sent to the function and will be used inside the timer.
    
        });
        var playAudio = function(audioElmnt, delay){
           setTimeout(function () {
                audioElmnt.play();
            }, delay);
        }
        setTimeout(function () {
            audioElement.currentTime = 0;
            audioElement.pause();
            audioElement.setAttribute('src', audioFileName);
            audioElement.load();
            audioElement.play();
        }, 500);
    });

    【讨论】:

      【解决方案2】:

      音频标签有一个你可以收听的事件

      audio.addEventListener('ended',function(e){ 
      });
      

      end 发出后,您可以开始列表中的下一个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        相关资源
        最近更新 更多