html5 播放音频循环 4 次然后停止 [英] html5 play audio loop 4x then stop

查看:39
本文介绍了html5 播放音频循环 4 次然后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将音频文件循环播放一定次数,即 4 次,我的声音文件循环正确,代码如下.?如何在 4 倍后停止循环,非常感谢 P

Hi I need to loop audio file a certain number of times ie 4, I have the sound file looping correctly, code below. ? how to stop loop after 4x, many thanks P

$('#play_audio').click(function () {
            var audio = document.createElement("audio");
            audio.src = "files/windows%20default.mp3";

            audio.addEventListener('ended', function () {
                // Wait 500 milliseconds before next loop

                setTimeout(function () { audio.play(); }, 500);
            }, false);           

            audio.play(); 
    });

推荐答案

我是这样做的:

 $('#play_audio').click(function () {                     
            var times = 4;
            var loop = setInterval(repeat, 500);

        function repeat() {
            times--;
            if (times === 0) {
                clearInterval(loop);
            }

            var audio = document.createElement("audio");
            audio.src = "files/windows%20default.mp3";

            audio.play();
        }

        repeat();           
    });      
}); 

这篇关于html5 播放音频循环 4 次然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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