#Autoplay when page loads

14 messages · Page 1 of 1 (latest)

halcyon basin
#

Hi im making a loading screen and i want to make it auto play when the page opens can some1 help me?

#
window.onload = 
    function auto() {
        musicContainer.classList.add('play');
        playBtn.querySelector('i.fas').classList.remove('fa-play');
        playBtn.querySelector('i.fas').classList.add('fa-pause');
      
        audio.play();
        audio.volume = 0.3;
      }
#

i tried this

#

but dint work

#

the idea is that this starts autoplaying

#

this is the css code

unborn ermine
#

Not sure if this is your issue, but many browsers block autoplay because users don't want things to just start playing and making noise on load. Something to consider.

halcyon basin
#

let me do a quick test

#

im gonna try this code
// Call playSong() when the page loads
window.addEventListener('load', playSong);
bcs its a loading screen so it can work i guess

unborn ermine
#

There's also rules on when it's allowed in chrome for example. But I don't remember the criteria

halcyon basin
#

for a loading screen in a game

halcyon basin
#

i think i got it working