100%

HTML5 Audio

R Proffitt
by Richard Proffitt
iopan design

The Audio tag, it's helper tags and it's attributes can achieve a lot in manipulating audio content. Note how the code below is rendered by your browser and take a look at how changing the coded attributes can alter the way in which the audio performs. First of all in this article, I'd like to look at the correct way for audio content to be installed on the web in HTML, then I'll be looking at different audio file types and which ones are best suited to web browsers. Finally I'll take a look at additional ways in which audio can be changed.

In the last part of this article you'll see that I'm making use of the HTML5 DOM* to do more media-manipulation, with the help of JavaScript. HTML5 defines DOM methods, properties, and events for the <audio> element. This allows you to load, play, and pause audios, as well as set duration and volume (amongst other things).

* “The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”
Take a look at the article: “HTML and the DOM” for more information.


An Audio Element Example

The following example shows how an audio clip can be looped when play has started

<audio controls loop>
  <source src="audio/erik_satie_3_gymnopedies.ogg" type="audio/ogg">
  <source src="audio/erik_satie_3_gymnopedies.mp3" type="audio/mpeg">
  <source src="audio/erik_satie_3_gymnopedies.wav" type="audio/wav">
  Your browser does not support the audio element.
</audio>

Notice how by adding the controls attribute you can add audio controls, like play, pause, and volume. By adding the loop attribute, the piece of music will continually loop back to the beginning of play (until the pause button is pressed).

There are other attributes that can be used such as autoplay which, as you might expect, start the audio playing as soon as the page has finnished loading. However, it's often considered poor netiquette to use the autoplay attribute in most everyday circumstances. Such practice can be very annoying to your users, use autoplay sparingly.


Audio Formats

MP3 is the newest format for compressed recorded music. The term MP3 has become synonymous with digital music and it's looking to be the internet's music format of choice. There remain, however, a large number of file formats that are also keen to corner the internet market, and some browsers are built with other formats in mind. Below is a list of music formats with a short description for each.

Format File Description
MIDI .mid
.midi
MIDI (Musical Instrument Digital Interface). Main format for all electronic music devices like synthesizers and PC sound cards. MIDI files do not contain sound, but digital notes that can be played by electronics. Plays well on all computers and music hardware, but not in web browsers.
RealAudio .rm
.ram
RealAudio. Developed by Real Media to allow streaming of audio with low bandwidths. Does not play in web browsers.
WMA .wma WMA (Windows Media Audio). Developed by Microsoft. Commonly used in music players. Plays well on Windows computers, but not in web browsers.
AAC .aac AAC (Advanced Audio Coding). Developed by Apple as the default format for iTunes. Plays well on Apple computers, but not in web browsers.
WAV .wav WAV. Developed by IBM and Microsoft. Plays well on Windows, Macintosh, and Linux operating systems. Supported by HTML5.
Ogg .ogg Ogg. Developed by the Xiph.Org Foundation. Supported by HTML5.
MP3 .mp3 MP3 files are actually the sound part of MPEG files. MP3 is the most popular format for music players. Combines good compression (small files) with high quality. Supported by all browsers.
MP4 .mp4 MP4 is a video format, but can also be used for audio. MP4 video is the upcoming video format on the internet. This leads to automatic support for MP4 audio by all browsers.

MP3, WAV, and Ogg audio are supported by the HTML5 Standard.


HTML5 defines DOM methods, properties, and events for the <audio> element. This allows you to load, play, and pause audios, as well as set duration and volume. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Cras tellus enim, sagittis aer varius faucibus, molestie in dolor. Mauris molliadipisg elit, in vulputate est volutpat vitae.

Sed vitae fermentum diam, eget sollicitudin felis. Vestibulum massa arcu, tempor sit amet mollis ac, eleifend auctor turpis Pellentesque feu giat quis turpis quis bibendum

Someone famous in Source Title

Cras tellus enim, sagittis aer varius faucibus, molestie in dolor. Mauris molliadipisg elit, in vulputate est volutpat vitae. Pellentesque convallis nisl sit amet lacus luctus vel consequat ligula suscipit. Aliquam et metus sed tortor eleifend pretium non id urna. Fusce in augue leo, sed cursus nisl. Nullam vel tellus massa. Vivamus porttitor rutrum libero ac mattis. Aliquam congue malesuada mauris vitae dignissim.

img

Fusce in augue

Cras tellus enim, sagittis aer varius faucibus, molestie in dolor. Mauris molliadipisg elit, in vulputate est volutpat vitae. Pellentesque convallis nisl sit amet lacus luctus vel consequat ligula suscipit. Aliquam et metus sed tortor eleifend pretium non id urna. Fusce in augue leo, sed cursus nisl. Nullam vel tellus massa. Vivamus porttitor rutrum libero ac mattis. Aliquam congue malesuada mauris vitae dignissim.

<div class="logo-footer-cont">
  <a href="index.html">
     <img class="logo-footer" src="images/logo-footer.png" alt="logo">
  </a>
</div>

Pellentesque venenatis tellus non purus tincidunt vitae ultrices tellus eleifend. Praesent quam augue, accumsan nec tempus dapibus, pharetra ac lacus. Nunc eleifend consequat justo id dapibus. In ut consequat massa. Nunc scelerisque suscipit leo nec imperdiet.

Nullam vel tellus

Pellentesque convallis nisl sit amet lacus luctus vel consequat ligula suscipit. Aliquam et metus sed tortor eleifend pretium non id urna. Fusce in augue leo, sed cursus nisl. Nullam vel tellus massa. Vivamus porttitor rutrum libero ac mattis. Aliquam congue malesuada mauris vitae dignissim. ❧



Articles