Embedding Audio in an HTML5 Document
Posted: Thu Mar 08, 2012 12:02 am
Hello guys,
Once again, I'll be making a quick HTML5 tutorial, this time about how to embed audio to an HTML5 document.
As I've said in previous tutorials, HTML5 brings simpler tags, and more meaning to the code. That said, embedding audio to a webpage is no more complicated than using the <audio> and </audio> tags.
Now, there is one unfortunate drawback: not all browsers support the same audio formats, as you can see below:
AAC (mp3): supported by IE, Safari and Chrome
OGG: supported by Firefox, Opera and Chrome
This means you can either choose the browser you want to support best, or include both to insure cross-browser compatibility.
Let's get to the code!
Start by making a simple HTML5 document. You can find out by reading my previous tutorial here.
In the body section of the document, you'll want to add an <audio> tag. Then, give it a 'controls' attribute, like so:
That's it! You'll now be able to play audio in your HTML5 document.
Enjoy!
Once again, I'll be making a quick HTML5 tutorial, this time about how to embed audio to an HTML5 document.
As I've said in previous tutorials, HTML5 brings simpler tags, and more meaning to the code. That said, embedding audio to a webpage is no more complicated than using the <audio> and </audio> tags.
Now, there is one unfortunate drawback: not all browsers support the same audio formats, as you can see below:
AAC (mp3): supported by IE, Safari and Chrome
OGG: supported by Firefox, Opera and Chrome
This means you can either choose the browser you want to support best, or include both to insure cross-browser compatibility.
Let's get to the code!
Start by making a simple HTML5 document. You can find out by reading my previous tutorial here.
In the body section of the document, you'll want to add an <audio> tag. Then, give it a 'controls' attribute, like so:
Code: Select all
Now, between the <audio> and </audio> tags, we'll want to put the source of our audio. This will be done inside a <source> tag.<audio controls> </audio>
Code: Select all
Notice the first attribute is set to "filename.mp3". This defines the path to the resource. Secondly, you'll see that the 'type' attribute is set to "audio/mpeg". This is because our file is of the mp3 type. If you were to link to an ogg file, you would put "audio/ogg" instead.<audio> <source src="filename.mp3" type="audio/mpeg" />
That's it! You'll now be able to play audio in your HTML5 document.
Enjoy!