HTML background music autoplay loop

Welcome to a short tutorial on how to add background music in HTML.

The fastest way to add background music to a website is to insert an audio tag at the bottom of the page .

Yep, its that simple, but there are still a couple of things to take note of Read on to find out!

I have included a zip file with all the example source code at the start of this tutorial, so you dont have to copy-paste everything Or if you just want to dive straight in.

REAL QUICK SLIDES

Firstly, here is the download link to the example code as promised.

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

QUICK NOTES

There is nothing to install, so just download and unzip into a folder. If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world If you need answers urgently, please check out my list of websites to get help with programming.

AUDIO MECHANICS

All right, let us now get into more details on the mechanics of the audio background music.

BACKGROUND MUSIC EXAMPLE

1-background-music.html
HTML Background Music

Contents here as usual.

Once upon a time, in the Stone Age of the Internet, we have to use all sorts of funny plugins to play audio. But yep, its really that simple now, no rocket science required.

  • Just use the tag, but try to place it near to the bottom of the page So that the audio loads last and users dont have to stare at an empty page for long; Let the text and images load first.
  • The autoplay property should be self-explanatory Automatically start playing when the audio file is loaded.
  • The loop property as well Automatically loop when the audio has ended.

PLAYING MULTIPLE FILES

2-many.html

If you have a whole playlist of songs, sandwich them between the tag, using multiple tags.

SUPPORTED FILE FORMATS

The above examples only used the widely supported mp3 file format, but please take note it is also OK to use the many other audio file formats wav, ogg, webm, flac. But the support of each file format varies from browser-to-browser Check out this table on Wikipedia on the supported audio coding formats.

AUTOPLAY IS NOT REALLY RELIABLE

Nope, please dont get me wrong. The HTML tag is widely supported in all modern browsers, and it should play just fine so long as the user is on a WIFI connection Meaning, the problem comes when users are on mobile devices or slow/unstable connection.

Every browser will deal with autoplay differently If the user is on a mobile device, the autoplay will most likely be ignored and not play. Also, we can also set some browsers to always ignore autoplay.

USEFUL BITS & LINKS

Thats all for this guide, and here is a small section on some extras and links that may be useful to you.

A NOTE ABOUT &

If you have been poking around the Internet, you might spot some tutorials using and . Please do not use those anymore They are deprecated and outdated. Stick with the modern instead.

BACKGROUND MUSIC NOT A GOOD IDEA?

Congratulations on finishing this tutorial But after all of the basics, I will not recommend putting any form of background music. Why?

  • Audio files will slow down the loading of the website.
  • It is not really reliable in any case It can be totally ignored by the browsers, or by the users setting.
  • Intrusive. Imagine being in a quiet office or library, and the page suddenly plays loud music.
  • Annoying. The user is listening to something else, and the background music on the page just crosses over.

So yep, not a good idea. Just dont include any background music, unless it is absolutely required for a good reason.

INFOGRAPHIC CHEAT SHEET

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Video liên quan

Chủ Đề