Home » Javascript » Randomize javascript audio variable on music finish

Randomize javascript audio variable on music finish

Posted by: admin August 19, 2018 Leave a comment


I have a start and stop buttons and I’m trying to randomize the song that will be played next if the start button is just pressed 1 time, but I keep getting the same music to be played over and over.

It randomizes fine if I press start, stop and start again, but not when the music finishes by herself – can somebody tell me what is wrong with the code?

var sounds = [

function StartOrStop(audioFile) {
    srcAudio = sounds[Math.floor(Math.random() * sounds.length)];
    var audie = document.getElementById("myAudio");
    audie.addEventListener('ended', function() {
        this.currentTime = 0;
    }, false);

    if (audie.paused == false) {
    } else {
        audie.src = sounds[Math.floor(Math.random() * sounds.length)];