javascript - 如何循环遍历多个音频文件并使用 javascript 顺序播放它们?

标签 javascript jquery html audio

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

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

$(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);
});

最佳答案

您不应该在循环中使用 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);
});

关于javascript - 如何循环遍历多个音频文件并使用 javascript 顺序播放它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37294380/

相关文章:

javascript - Ajax 内容加载数据库值不起作用

Javascript 通过月份名称获取月份范围

javascript - 如何防止将鼠标悬停在链接上时弹出框?

javascript - 尝试使用 ajax 查找图像时不断出错?

javascript - d3节点标记: how to display JSON data formatted with HTML?

javascript - 使用 div 重新创建行为 "background-attachment: fixed"

Css 可重复属性

html - CSS 链接到 HTML

javascript - 与 RTL 语言一起使用时字符串替换函数调用的顺序

javascript - 在 Highcharts 中格式化工具提示数字