用Javascript循环音频onclick

时间:2017-05-22 09:15:47

标签: javascript loops audio onclick

我正在为作业创建一个启动板,并努力找到一种循环播放方式和点击音频剪辑,然后再次点击时停止音频。我有这个代码,但它没有循环atm(我是Javascript的新手,因此可能需要向我解释)。

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Title</title>
            <link rel="stylesheet" href="css/style.css">
            <script> var one = new Audio(); var two = new Audio(); var three = new Audio(); var four = new Audio();
            var five = new Audio(); var six = new Audio(); var seven = new Audio(); var eight = new Audio();
            one.src = "files/audio/one.wav"; two.src = "files/audio/two.wav"; three.src = "files/audio/three.wav";
            four.src = "files/audio/four.wav"; five.src = "files/audio/five.wav"; six.src = "files/audio/six.wav";
            seven.src = "files/audio/seven.wav"; eight.src = "files/audio/eight.wav";
            </script>
        </head>


<div class="parent">
        <div class="item" onmousedown="one.play()">Bass</div>
        <div class="item" onmousedown="two.play()">Bass</div>
        <div class="item" onmousedown="three.play()">Bass</div>
        <div class="item" onmousedown="four.play()">Beat</div>
        <div class="item" onmousedown="five.play()">Beat</div>
        <div class="item" onmousedown="six.play()">Beat</div>
        <div class="item" onmousedown="seven.play()">Drum</div>
        <div class="item" onmousedown="eight.play()">Drum</div>
</div>

1 个答案:

答案 0 :(得分:3)

如其他SO帖子所示,Web Audio元素具有可以设置为true的循环属性 Loop audio with JavaScriptJavascript Repeating Audio

对于您的每个音频实例,您需要将其设置为true。 e.g。

one.loop = true;
two.loop = true;