CedricParkerLewis
CedricParkerLewis

Reputation: 403

Is there any reliable solution to make background music autoplay in a HTML page?

I am trying to make a music automatically playing when a HTML page is loaded. I tried different things but without success.

I searched through stackoverflow to find a good solution, but didn't manage to find a strong workaround that works with all browsers.

Any help would be greatly appreciated !

Best Regards, Cedric

Upvotes: 0

Views: 909

Answers (4)

Ramki
Ramki

Reputation: 472

<audio controls autoplay>
  <source src="music.mp3" type="audio/mpeg">
</audio>

Upvotes: 0

Jeppe Hasseriis
Jeppe Hasseriis

Reputation: 310

While the autoplay attribute works on some cases, you should know that in many cases it doesn't. I suggest you read this MDN article to understand why and also get some ideas how to adapt to the limitations.

Upvotes: 1

Vinod Venugopal
Vinod Venugopal

Reputation: 41

I think this code will work for you ! Works perfectly fine for me!

<audio controls autoplay> <source src="audiofile.mp3" type="audio/mpeg"> </audio>

Upvotes: 0

Mathieu
Mathieu

Reputation: 759

The <audio> element with the autoplay attribute should be sufficient. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio

It is extremely well supported.

Upvotes: 0

Related Questions