HTML 5 <audio> & <video> elements

April 3, 2009 by David Corvoysier

One of the most revolutionary concept introduced by HTML 5 is the native support of audio and video playback in web pages.

Two new HTML elements have been defined for that purpose: <audio> and <video>, based on the generic media element type.

Media elements use a common set of attributes:

  • src: defines the source of the media to render,
  • autoplay: boolean to activate|prevent immediate playback,
  • loop: boolean to activate|prevent automatic looping on content,
  • controls: boolean to display|hide browser playback user interface.

<video> and <audio> elements can contain elements to be displayed to the user in older browsers that do not support these new elements.

Below is an example of how to use the <audio> element:

<audio src="audio.ogv" controls width="100" height="50">
    <a href="audio.ogv">Listen</a>
</audio>

Alternatively to the src attribute, the content to be rendered can be specified using one or more <source> elements, each of them describing a specific media resource.

Each <source> can be identified by its src and type attributes, optionally complemented by a codecs attribute:

<audio controls width="100" height="50">
     <source src="audio.ogg" type="audio/ogg; codecs=vorbis">
    <source src="audio.spx" type="audio/ogg; codecs=speex">
    <source src="audio.oga" type="audio/ogg; codecs=flac">
</audio>

The <video> element has an optional poster attribute that can be used to define an image to be displayed before the video is played:

<video src="oggy.ogv" poster="oggy.jpg" />

These new media elements also support a set of handy javascript method, properties and events, to allow the fine-grained control of the rendered UI.

<script>
function playVideo(url){
     var video = document.getElementById("video");
     video.setAttribute("src",url);
}
</script>
<video id="videoPlayer" controls=false/>
<p>
     <button type="button"
             onclick="video.playbackRate = -2;">
         Rew
     </button>
     <button type="button"
             onclick="video.play();">
         Play
     </button>
     <button type="button"
             onclick="video.pause();">
         Pause
     </button>
     <button type="button"
                 onclick="video.playbackRate = 2;">
         FF
     </button>
</p>

5 Comments »

  1. I am having an issue with duration… I use a php file as the source, so people cannot just go and download the audio file, it uses readfile(); to output the code of the mp3 track I wish to play. The javascript audio.duration doesn’t work because it thinks that it is a stream, though it works just fine if I have the source as an mp3 file instead.

    What should I do? I don’t want people to be able to download my mp3s

    Comment by Charles — December 29, 2009 @ 5:11 pm

  2. Off the top of my head, I would say it may be related to the HTTP headers you send with the file: have you checked that the mime-type and size are correctly set ?

    Comment by David Corvoysier — December 30, 2009 @ 9:29 pm

  3. As with Charles I’ve noticed that giving an audio element a .php file that outputs readfile data of an mp3, even with audio/mpeg headers, that the audio element can play, but will not be able to start/stop at different time positions.

    Comment by reelfernandes — August 20, 2010 @ 6:00 pm

  4. The width and height do not seem to work in Safari on a Mac. Do you have an example page?

    Comment by Linda — September 16, 2010 @ 5:36 pm

  5. If you have multiple audio controls on a page, is there a way to actually “stop” one from playing (not pause) or reset it before the second one plays?

    Comment by Linda — September 16, 2010 @ 5:38 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment