I have a piece of JavaScript that is supposed to loop through some audio files in series that are defined using audio tags with an id in the HTML body. The problem is that the audio at first would stop in the first 3 seconds, and now it doesn't play at all, and I can't find what's wrong. The below code is all in the HTML body and is the smallest possible example of my error:
<audio id="lobby1">
<source src="audio/lobby-audio1.mp3" type="audio/mpeg">
</audio>
<audio id="lobby2">
<source src="audio/lobby-audio2.mp3" type="audio/mpeg">
</audio>
<button onclick="playLobby()">play lobby</button>
<script>
number = 1
lobbySong = "lobby" + number
function playLobby() {
while (number < 2) {
var lobbySong = document.getElementById(lobbySong);
lobbySong.play();
lobbySong.onended = function() {
number++
};
}
}
</script>
The error I get from the console is cannot read property 'play' of null referring to lobbySong.play in the while loop.
while (number < 2) {will run continuously, not waiting for onended, executing play at an extremely fast pacealert()workedonclicklobbySong = "lobby" + numberwait(lobbySong.duration*1000);without waiting for theonended