0

I am creating a launchpad for an assignment, and struggled to find a way to loop and audio clip on click, and then stop the audio when clicked again. I have this code but it doesn't loop atm (I'm new to Javascript so it may need explaining to me).

<!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 Answer 1

3

As shown on other SO posts, the Web Audio element has a loop property you can set to true Loop audio with JavaScript and Javascript Repeating Audio

for each of your instances of Audio, you will need to set it to true. e.g.

one.loop = true;
two.loop = true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.