How can I add music in html5? No solutions are working

I need to add a soundtrack to my game. The audio file is m4a.

According to Replit AI (don’t know if I’m allowed to use its responses in a post): To loop background music in JavaScript, you can use the <audio> HTML element along with JavaScript to control the playback. Here’s an example of how you can loop a background music file in the script.js file:


// Get a reference to the audio element
const audio = new Audio("path/to/background-music.mp3");
// Set the loop attribute to true for looping
audio.loop = true;
// Play the background music;

Make sure to replace "path/to/background-music.mp3" with the actual path to your background music file.

Hope this helps :smiley:


The copying and pasting messed up a bit lol.

Doesn’t seem to work for me, does it work for you?

I can try converting it to mp3.

Didn’t work. It could just be me.

Sound and video in HTML will not play until once user action happens. So basically, you’ll need to add something that either says “click to enable sound” or “Click to begin game”. Either will confirm that the user is active and let you play audio. (If I’m understanding correctly, you are trying to autoplay sound as soon as the user enters your website.)