My goal is to create an app that plays a sound based on a number input. I have several short MP3 audio files for different numbers, and I want the app to play these sounds in sequence. However, when I try to do this, only the last sound corresponding to the final number is played, and I encounter an error message in the console:
"Uncaught (in promise) DOMException: The play() request was interrupted by a new load request."
I'm unsure of what I am missing or if it's even possible to achieve this. Any assistance would be greatly appreciated.
function playSound(note){
var currentPlayer;
var player = document.getElementById("player");
var isPlaying = player.currentTime > 0 && !player.paused && !player.ended
&& player.readyState > 2;
if (!isPlaying){
player.src = "sounds/"+note+".mp3";
player.play();
}else{
player.pause();
player.currentTime = 0;
currentPlayer = player;
}
}
//variable with numbers where each number should load a sound and play
var numString = "0934590042529689108538569377239609480456034083552";
for(i = 0; i < numString.length; i++){
switch (parseInt(numString[i])){
case 1:
playSound("C");
break;
case 2:
playSound("D");
break;
case 3:
playSound("E");
break;
case 4:
playSound("F");
break;
case 5:
playSound("G");
break;
case 6:
playSound("A");
break;
case 7:
playSound("B");
break;
case 8:
playSound("C2");
break;
case 9:
playSound("D2");
break;
case 0:
playSound("silence");
break;
}
The Html:
<audio controls id="player" style="display: none">
<source src="#"></source>
</audio>