how to load audio file in a function call without any human interaction. Is this possible only using the javascript? I have tried this code below but it is not working -
const music = new Audio("audiofile.mp3");
function playMusic(){
music.play();
};
playMusic();
I think you want audio to play automatically without any user actually playing the audio? if so you can do:
const music = new Audio("audiofile.mp3");
window.onload = function playMusic(){
music.play();
};
this will play the audio as soon as the page is loaded, alternatively you can use event listeners to play the audio when certain events happen on the page.