noo
noo

Reputation: 149

How to make play/pause music button to loop music?

I created a play/pause button that plays music with HTML and Javascript. I'd like to know if there's a way to make the music start again when it's over, infinitely.

Here's my code (also https://jsfiddle.net/gp7yf1t3/):

HTML:

<audio id="player">
  <source src='https://audio.jukehost.co.uk/c926ef6560961d5fd02e35e1488a5997e8217bc1/1a1c12c319a' type='audio/mpeg'/>
</audio>

<button id="button">&#9658</button>

Javascript:

var button = document.getElementById("button");
var audio = document.getElementById("player");

button.addEventListener("click", function(){
  if(audio.paused){
    audio.play();
    button.innerHTML = "&#10074;&#10074;";
  } else {
    audio.pause();
    button.innerHTML = "&#9658";
  }
});

Thanks!

Upvotes: 2

Views: 440

Answers (1)

kushal parikh
kushal parikh

Reputation: 851

just set loop attribute

<audio id="player" loop>
  <source src='https://audio.jukehost.co.uk/c926ef6560961d5fd02e35e1488a5997e8217bc1/1a1c12c319a' type='audio/mpeg'/>
</audio>

Upvotes: 1

Related Questions