Error when sound loading?

So, yeah it sometimes plays and sometimes doesn’t.

The reason you are not seeing the audio box is that I implemented it should be auto-playing
Now, when checking the performance tab it just shows this under the audio that is supposed to play.

image
URL: https://visualkitten.cf

since I have no insight into the implementation of your audio file
I assume this is the cause

The most common reason is because all the major browsers are blocking
autoplay if this condition is not met
The user has interacted with the site (by clicking, tapping, pressing keys, etc.)

4 Likes

Adding to what OxyDac said, you can easily make a function that plays the file when the user interacts with the page.

let hasPlayed = false;
let audio = new Audio();
audio.src = "YourAudioFile.mp3";
function playOnClick(){
   if(hasPlayed){
      return;
   }
   audio.play();
   hasPlayed = true;
}
//In the case they enforced audio.
try{
   audio.play();
}
catch(err){
   //If they didn't interact with the page first and they didn't enforce allowing audio then it throws an error.
   //If that happened here then just wait for them to click and then play the audio
   document.addEventListener("click",playOnClick);
}
2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.