Search code examples
playbackautoplay

The MP3 file does not play in the page I mean autoplay failure


I wrote this kod

In HTML part:

Audio src="/true.mp3"></audio

In script tag:

window.addEventListener("DOMContentLoaded", event => {

const audio = document.querySelector("audio");

audio.volume = 5;

audio.play();
});

I have an MP3 file. When I open the page, all codes run but this part is not.


Solution

  • It can be autoplayed without javascript.
    Use <audio src="/true.mp3" autoplay></audio> to autoplay audio.


    Other controls:
    And you can also use controls to show controls, and loop to loop the audio.
    But user need to interact with the site.