【发布时间】: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