如何发挥每个音频元素的网页(HTML,JQuery的,使用Javascript) [英] How to play each audio element on a page (HTML, JQuery, Javascript)

查看:97
本文介绍了如何发挥每个音频元素的网页(HTML,JQuery的,使用Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

O.k我有上有多个音频元素的页面。我正在寻找一种方式来发挥每个人,从第一个开始,等待它完成播放,然后移动到下一个音频元素,直到最后一个元素。是否有任何可能的方式做到这一点?谢谢!

O.k I have a page that has multiple audio elements on it. I am looking for a way to play each one, starting from the first, waiting for it to finish playing and then move on to the next audio element, until the last element. Is there any way possible to accomplish this? Thanks!

编辑:

所以这是一个空文件之前,我不能添加一个code样品,因为音频元素都是由PHP动态添加的。

I can't add a code sample because the audio elements are all added dynamically by PHP so it's a empty file before that.

推荐答案

根据@利亚姆 - schauerman答案,我其实这种解决方案提出了:

Based on @liam-schauerman answer, I actually came up with this solution:

var index = 0;
var audioElements = document.getElementsByTagName('audio');

function playNext(index) {
    audioElements[index].play();
    $(audioElements[index]).bind("ended", function(){
        index++;
        if(index < audioElements.length){
            playNext(index);          
        }
    });

}

$(document).ready(function() {
    $("#swig").click(function() {
        audioElements[index].play();

        $(audioElements[index]).bind("ended", function(){
             index = index + 1;
             if(index < audioElements.length){
                playNext(index);          
             }                        
        });


    });
});

我只是一味codeD这一点。如果有人将阐述为什么这工作我会AP preciate它。

I just blindly coded this. If someone would elaborated and why this works I would appreciate it.

这篇关于如何发挥每个音频元素的网页(HTML,JQuery的,使用Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆