Surfin' Safari

HTML5 Media Support

Posted by Antti Koivisto on Monday, November 12th, 2007 at 2:14 pm

Another nice feature from the HTML5 draft specification is now available in the WebKit nightly builds for Mac OS X. The new HTML5 <video> and <audio> elements add native support for embedding video and audio content in web pages. They also provide a rich scripting API for controlling playback. Adding video to a web page is almost as simple as adding an image:

<video src=sample.mov autoplay></video>

To make a button that gives the user basic playback controls you could do this: 

<script>
function playPause() {
  var myVideo = document.getElementsByTagName('video')[0];
  if (myVideo.paused)
    myVideo.play();
  else
    myVideo.pause();
}
</script>
<input type=button onclick="playPause()" value="Play/Pause">

The specification also defines a set of events that can be used to react to changes in media playback and load state. For example:

myVideo.addEventListener('ended', function () {
  alert('video playback finished')
} );

To play audio from JavaScript you can simply do this:

var audio = new Audio("song.mp3");
audio.play();

The implementation is still a work in progress and not all features (including the ‘controls’ attribute which gives native playback controls) of the specification are there yet. The current implementation supports all formats that QuickTime supports, including installed 3rd party codecs.

The example below uses the ‘poster’ attribute of the <video> element to display an initial image before the video is loaded, progress events to track loading, and play/pause/ended events to make the overlay button reflect the video’s state.

>
+

13 Responses to “HTML5 Media Support”

  1. Hendrik.Runte Says:

    Thank you so much! This object-and-embed-stuff has been really a PITA, so far. The only workaround, a flash object (flv player), is reliable but not scriptable as your solution. Cheers!

  2. mrael2 Says:

    Sigh. I know this has probably been debated ad nauseam in other places, but HTML markup is terrible and inconsistent. Why did you choose to have a closing tag in the example but not quoted attributes? Using XHTML, the markup is far more consistent and gives a better example to others who eventually will want to serve their XHTML5 with the proper MIME type (application/xhtml+xml).

    <video src=”sample.mov” autoplay=”true”/> is much more consistent and instructive.

  3. Mark Rowe Says:

    @mrael2: This isn’t an appropriate place to debate the merits of HTML vs XHTML. Antti’s example is valid HTML 5 which is as much as is relevant to the topic.

  4. Pingback from <video> support in browsers » Broken Links:

    [...] has experimental support. Opera has experimental support. And now, Webkit has experimental support. The new HTML5 <video> element is getting support from a large part of the browser [...]

  5. ain Says:

    Very nice. What about streaming?

  6. Stuart Langridge Says:

    Does this new video support include Ogg Theora, as recommended in the HTML 5 spec, or does it only play video for which you already have codecs installed on your machine?

  7. antti Says:

    Streaming works in principle but has not been tested much yet. Some events may not work as expected.

    Ogg should work fine if you have a suitable codec installed. You can use <source> elements to give alternative formats, the implementation will pick the first one that is supported.

  8. Pingback from Javascript News » Blog Archive » HTML5 Media Support: video and audio tags and scriptability:

    [...] keeps on trucking and has added support for the HTML 5 media tags such as <video> and [...]

  9. Pingback from Webkit Introduces: HTML5 Media Support at Programming in the State of 70% Drunkeness:

    [...] Webkit just annonced that they are working on media tag support based on the HTML5 draft. It is currently implemented on Mac build and is based on the support of Quicktime. Another nice feature from the HTML5 draft specification is now available in the WebKit nightly builds for Mac OS X. The new HTML5 and elements add native support for embedding video and audio content in web pages. They also provide a rich scripting API for controlling playback. Adding video to a web page is almost as simple as adding an image [...]

  10. Pingback from HTML5 Media Support from WebKit and Opera | Ajaxus place on the net:

    [...] nice feature from the HTML5 draft specification is now available in the WebKit nightly builds for Mac OS X. The new HTML5 and elements add native support for embedding video [...]

  11. Pingback from I find your lack of DOCTYPE disturbing - Alpha Channel - msnbc.com:

    [...] The longer answer is a bit more complicated. We could, of course, just leave legacy pages without a DOCTYPE and carry on, boldly into the future, but some pages (like stories) still use components that don’t expect a DOCTYPE and break the page rather horribly. We’re also having to reconfigure our CMS to support DOCTYPES properly – a task that’s actually more difficult than writing a line of markup at the top of the page. For instance, to be XHTML compliant, we have several dozen legacy bugs to fix to be considered valid XML. I’m personally lobbying for HTML 5, so, Safari 3 users, get ready. [...]

  12. Pingback from A Preview of HTML 5 « N.T.C’s Weblog:

    [...] Opera and WebKit have released builds with partial support for the video element. You may download the experimental [...]

  13. Pingback from HTML 5 does the hokey pokey:

    [...] addition. And if you can’t wait that long and just have to jump into the thicket, both Opera and Webkit have experimental builds with some [...]