Conditional does not work correctly

0

At the beginning of my page, there is a song that plays automatically. When it ends, it should go to the next song, however I do not understand why it is not doing so, if in my conditional I place it, when the song ends, the next one starts. The alert within my function does not appear.

The code is as follows:

currentTrack = 0

beats = ['eminem.mp3', 'icecube.mp3', 'lilwayne.mp3', 'nas.mp3', 'still.mp3', 'today.mp3']

beat = document.getElementById('beat')

if(beat.ended == true){

function reproducir(){



alert("estoy dentro de la funcion")
	


nextTrack = beats[currectTrack++]
beat.setAttribute('src', nextTrack)
beat.play

})

}
<audio src="../estudio/beats/eminem.mp3" autoplay controls id="beat"></audio>
<button onclick="reproducir()">iniciar</button>
<button onclick="detener()">detener</button>
<div id="seccion"></div>

Why is that happening? How can I solve it?

    
asked by steven 13.04.2017 в 15:08
source

1 answer

0

As an observation, that conditional is always assigning the last track to the player. If that is not the desired effect, correct it.

The problem is that this conditional is only executed once , when reproducir is executed, that is, when the Start button is pressed. If you want to automatically go to the next song you must use the event end :

let currentTrack = 0; // lleva un control de la pista actual

beat.addEventListener('end', e => {
  const nextTrack = beats[currentTrack++];
  beat.setAttribute('src', nextTrack);
  beat.play();
}

If, on the other hand, you want to pause and then start again, change tracks, the event you need is pause :

beat.addEventListener('pause', e => {
  const nextTrack = beats[++currentTrack];
  beat.setAttribute('src', nextTrack);
  beat.play();
};

In this way, when you pause the player, the song index increases and when you start the lesson again you get a new track and execute it.

    
answered by 13.04.2017 в 15:50