Reputation: 149
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">►</button>
Javascript:
var button = document.getElementById("button");
var audio = document.getElementById("player");
button.addEventListener("click", function(){
if(audio.paused){
audio.play();
button.innerHTML = "❚❚";
} else {
audio.pause();
button.innerHTML = "►";
}
});
Thanks!
Upvotes: 2
Views: 440
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