vendredi 7 avril 2017

How to get a random song to play using an array in javascript?

I'm using html and javascript to play a specific song. However I want it to play a random song and im trying to do that by putting the songs into an array and randomly choosing a song from the array. however its not working and i think its the function. any help?

HTML:

<audio id="mytrack" controls>
    <source src="Song1.wav" type = "audio/wav"/>
</audio>

<audio id="gd" controls>
    <source src="Song2.mp3" type = "audio/mp3"/>
</audio>

JavaScript:

var mytrackJS = document.getElementById('mytrack');
var playButtonJS = document.getElementById('playButton');
var gdJS = document.getElementById('gd');

var = audioPlaying [mytrackJS, gdJS];
var rand = audioPlaying[Math.floor(Math.random() * audioPlaying.length)];


playButtonJS.addEventListener('click', playOrPause, false);

function playOrPause() {
if (!rand.paused && !rand.ended){
    rand.pause();
    playButtonJS.style.backgroundImage = 'url(playbutton.svg)';
}
else{
    rand.play();
    playButtonJS.style.backgroundImage = 'url(pausebutton.png)';
}

}




Aucun commentaire:

Enregistrer un commentaire