Skip to main content

Using The HTML5 Audio Tag To Add Background Music To Any Webpage




The HTML5 Audio Tag


The HLTM5 Audio Tag can be used to add background music to any website. The code provided in this tutorial will work on any platform. I will demonstrate the HTML5 Audio functionality right in this blog post.

As you can see in the lower left side of this page, there is a speaker icon on a white background. If you hover this icon the HTML5 audio player, playing the background music for this tutorial, is displayed.




HTML5 Audio Attributes We Will Use In This Tutorial


For the purpose of this tutorial we will want the HTML5 Audio tag to preload the song, we will also want it to displays the audio controls, autoplay and loop the audio.

So the audio tag we will use will look something like this:

<audio controls autoplay preload loop>
...
</audio>




Adding The Audio


We will add the audio track in both mp3 and ogg format so that we can get cross browser compatibility. Not all browsers support mp3 and not all browsers support ogg. But using them together we can cover all major browsers.

If you have your audio file in mp3 format i recommend using an open source tool like Audacity to convert the file to ogg format. So our code now looks like this:

<audio autoplay="" controls="" loop="" preload="">
<source src="http://192.254.184.234/~tangh/01_admin_resources/Shimmer/ogg/Smile.ogg" type="audio/ogg"></source>
<source src="http://192.254.184.234/~tangh/01_admin_resources/Shimmer/mp3/Smile.mp3" type="audio/mpeg"></source>
Your browser does not support the audio element.
</audio>

Change the src to use your own files. I have used a song from the free Shimmer Album. We will also mention this "Your browser does not support the audio element" for browsers which are not up to date.




Get The Complete Code


If you want to get the exact same style used in this tutorial you can get the entire code below:

<style media="screen" type="text/css">
#backgroundaudio {
display: block;
position: fixed;
bottom: -43px;
left: 5px;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#backgroundaudio:hover {
bottom: 0;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#backgroundaudio audio {
background: #ffffff;
padding: 5px;
display: table-cell;
vertical-align: middle;
height: 43px;
z-index: 9998;
}
#backgroundaudio i {
font-size: 40px;
display: block;
background: #ffffff;
padding: 5px;
width: 50px;
float: none;
margin-bottom: -1px;
z-index: 9999;
}
</style>
<div id="backgroundaudio">
<i class="icon-volume-up"></i>
<audio autoplay="" controls="" loop="" preload="">
<source src="http://192.254.184.234/~tangh/01_admin_resources/Shimmer/ogg/Smile.ogg" type="audio/ogg"></source>
<source src="http://192.254.184.234/~tangh/01_admin_resources/Shimmer/mp3/Smile.mp3" type="audio/mpeg"></source>
Your browser does not support the audio element.
</audio>
</div>


Please leave your questions and suggestions in the comments section below.

Comments

Popular posts from this blog

iPhone 5 Rumor Roundup

Release Date The latest rumors regarding the iPhone 5 indicate that the official release date will be officially announced during the next Apple event which is to be held on September 12th 2012. According to Verizon CFO, Fran Shammo, Apple (AAPL) will stick to last year's release date and launch the iPhone 5 in October 2012. Although during the 2012 WWDC we had the opportunity to get some more details about iOS 6, no hardware specifications were released. source - 9to5mac.com Larger Screen The iPhone 5 is rumoured to have a larger, 4 inch retina display. source - Apple.pro Nano SIM According to the Financial Times, mobile carriers in Europe are stocking up on Nano SIM cards. This might be related to the release of the next generation iPhone. Smaller Dock Connector Another wide spread rumour is that Apple will release the iPhone 5 with a smaller, 8 pin dock connector. This means that new cables will be available for  new Apple devices released in the future. Did you hear any new rum...

Single Page HTML5 CSS3 Business Card Template 2.0

After seeing a huge interest in the Single Page Business Card Tutorial  i decided to create a Single Page Business Card 2.0 Template. Besides listing your title and contact details the 2.0 template also has some nice CSS3 transition effects, is available in 4 colors and the best part: IT'S FREE FOR DOWNLOAD . Demo Download CSS3 Transition Effect On hover the Name/Title block and the Contact Details blocks slide out vertically showing a centered block containing name and position, social and contact icons. Red Template Demo Blue Template Demo Yellow Template Demo Green Template Demo

HTML Codes for Romanian Characters

Character Simple Code Numerical Code Hex Code Ă &#258; &#x102; ă &#259; &#x103; Â &Acirc; &#194; &#xC2; â &acirc; &#226; &#xE2; Î &Icirc; &#206; &#xCE; î &icirc; &#238; &#xEE; Ș &#x218; &#xDA; ș &#x219; &#xDB; Ş &#350; &#x15E; ş &#351; &#x15F; Ţ &#354; &#x162; ţ &#355; &#x163;