Introducing HTML5: Video and Audio
- Native multimedia: why, what, and how?
- Codecsthe horror, the horror
- Rolling custom controls
- Multimedia accessibility
- Summary
Bruce Lawson and Remy Sharp
A Long Time Ago, in a galaxy that feels a very long way away, multimedia on the Web was limited to tinkling MIDI tunes and animated GIFs. As bandwidth got faster and compression technologies improved, MP3 music supplanted MIDI and real video began to gain ground. All sorts of proprietary players battled it out—Real Player, Windows Media, and so on—until one emerged as the victor in 2005: Adobe Flash, largely because of the ubiquity of its plugin and the fact that it was the delivery mechanism of choice for YouTube.
HTML5 provides a competing, open standard for delivery of multimedia on the Web with its native video and audio elements and APIs. This chapter largely discusses the <video> element, as that's sexier, but most of the markup and scripting are applicable for both types of media.
Native multimedia: why, what, and how?
In 2007, Anne van Kesteren wrote to the Working Group:
"Opera has some internal experimental builds with an implementation of a <video> element. The element exposes a simple API (for the moment) much like the Audio() object: play() , pause() , stop() . The idea is that it works like <object> except that it has special <video> semantics much like <img> has image semantics."
While the API has increased in complexity, van Kesteren's original announcement is now implemented in all the major browsers, and during the writing of this book Microsoft announced forthcoming support in Internet Explorer 9.
An obvious companion to a <video> element is an <audio> element; they share many similar features, so in this chapter we discuss them together and only note the differences.
<video>: Why do you need a <video> element?
Previously, if developers wanted to include video in a web page, they had to make use of the <object> element, which is a generic container for "foreign objects." Due to browser inconsistencies, they would also need to use the previously invalid <embed> element and duplicate many parameters. This resulted in code that looked much like this:
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/9sEI1AUFJKw&hl=en_GB&fs=1&"></param>
<param name="allowFullScreen"
value="true"></param>
<param name="allowscriptaccess"
value="always"></param>
<embed src="https://www.youtube.com/v/9sEI1AUFJKw&hl=en_GB&fs=1&"
type="application/x-shockwave-flash"
allowscriptaccess="always"
allowfullscreen="true" width="425"
height="344"></embed>
</object>
This code is ugly and ungainly. Worse than that is the fact that the browser has to pass the video off to a third-party plugin; hope that the user has the correct version of that plugin (or has the rights to download and install it, or the knowledge of how to); and then hope that the plugin is keyboard accessible—along with all the other unknowns involved in handing the content to a third-party application.
Plugins can also be a significant cause of browser instability and can create worry in less technical users when they are prompted to download and install newer versions.
Whenever you include a plugin in your pages, you're reserving a certain drawing area that the browser delegates to the plugin. As far as the browser is concerned, the plugin's area remains a black box—the browser does not process or interpret anything that is happening there.
Normally, this is not a problem, but issues can arise when your layout overlaps the plugin's drawing area. Imagine, for example, a site that contains a movie but also has JavaScript or CSS-based dropdown menus that need to unfold over the movie. By default, the plugin's drawing area sits on top of the web page, meaning that these menus will strangely appear behind the movie.
Problems and quirks can also arise if your page has dynamic layout changes. If the dimensions of the plugin's drawing area are resized, this can sometimes have unforeseen effects—a movie playing in the plugin may not resize, but instead simply be cropped or display extra white space. HTML5 provides a standardised way to play video directly in the browser, with no plugins required.
One of the major advantages of the HTML5 video element is that, finally, video is a full-fledged citizen on the Web. It's no longer shunted off to the hinterland of <object> or the non-validating <embed> element.
So now, <video> elements can be styled with CSS; they can be resized on hover using CSS transitions, for example. They can be tweaked and redisplayed onto <canvas> with JavaScript. Best of all, the innate hackability that open web standards provide is opened up. Previously, all your video data was locked away; your bits were trapped in a box. With HTML5 multimedia, your bits are free to be manipulated however you want.
What HTML5 multimedia isn't good for
Regardless of the somewhat black and white headlines of the tech journalists, HTML5 won't "kill" all plugins overnight. There are use-cases for plugins not covered by the new spec.
Copy protection is one area not dealt with by HTML5—unsurprisingly, given that it's a standard based on openness. So people who need DRM are probably not going to want to use HTML5 video or audio, as they will be as easy to download to a hard drive as an <img> is now. Some browsers offer simple context-menu access to the URL of the video, or even to save the video. (Of course, you don't need us to point out that DRM is a fools' errand, anyway. All you do is alienate your honest users while causing minor inconvenience to dedicated pirates.)
There is a highly nascent <device> element rudimentarily specified for "post-5" HTML, but there is no support in browsers for it. Plugins remain the best option for a browser to transmit video and audio from the user's machine to a web page such as Daily Mugshot or Chat Roulette. After shuddering at the unimaginable loneliness that a world without Chat Roulette would represent, consider also the massive amount of content out there that will require plugins to render it for a long time to come.
Anatomy of the video element
At its simplest, including video on a page in HTML5 merely requires this code:
<video src=turkish.ogv></video>
The .ogv file extension is used here to point to an Ogg Theora video.
Similar to <object>, you can put fallback markup between the tags, for older Web browsers that do not support native video. You should at least supply a link to the video so users can download it to their hard drives and watch it later on the operating system's media player. Figure 4.1 shows this code in a modern browser and fallback content in a legacy browser.
Figure 4.1 HTML5 video in a modern browser and fallback content in a legacy browser.
<h1>Video and legacy browser fallback</h1>
<video src=leverage-a-synergy.ogv>
Download the <a href=leverage-a-synergy.ogv>How to leverage a synergy video</a>
</video>
However, this example won't actually do anything just yet. All you can see here is the first frame of the movie. That's because you haven't told the video to play, and you haven't told the browser to provide any controls for playing or pausing the video.
autoplay
You can tell the browser to play the video or audio automatically, but you almost certainly shouldn't, as many users (and particularly those using assistive technology, such as a screen reader) will find it highly intrusive. Users on mobile devices probably won't want you using their bandwidth without them explicitly asking for the video. Nevertheless, here's how you do it:
<video src=leverage-a-synergy.ogv autoplay>
</video>
controls
Providing controls is approximately 764 percent better than autoplaying your video. See Figure 4.2. You can use some simple JavaScript to write your own (more on that later) or you can tell the browser to provide them automatically:
<video src=leverage-a-synergy.ogv controls>
</video>
Figure 4.2 The default controls in Firefox 3.6 (similar in all modern browsers).
Naturally, these differ between browsers, in the same way that form controls do, for example, but you'll find nothing too surprising. There's a play/pause toggle, a seek bar, and volume control.
Notice that these controls appear when a user hovers over a video or when she tabs to the video. It's also possible to tab through the different controls. This native keyboard accessibility is already an improvement on plugins, which can be tricky to tab into from surrounding HTML content.
If the <audio> element has the controls attribute, you'll see them on the page. Without the attribute, nothing is rendered visually on the page at all, but is, of course, there in the DOM and fully controllable via JavaScript and the new APIs.
poster
The poster attribute points to an image that the browser will use while the video is downloading, or until the user tells the video to play. (This attribute is not applicable to <audio>.) It removes the need for additional tricks like displaying an image and then removing it via JavaScript when the video is started.
If you don't use the poster attribute, the browser shows the first frame of the movie, which may not be the representative image you want to show.
height, width
These attributes tell the browser the size in pixels of the video. (They are not applicable to <audio>.) If you leave them out, the browser uses the intrinsic width of the video resource, if that is available. Otherwise it is the intrinsic width of the poster frame, if that is available. Otherwise it is 300 pixels.
If you specify one value, but not the other, the browser adjusts the size of the unspecified dimension to preserve the video's aspect ratio.
If you set both width and height to an aspect ratio that doesn't match that of the video, the video is not stretched to those dimensions but is rendered "letter-boxed" inside the video element of your specified size while retaining the aspect ratio.
loop
The loop attribute is another Boolean attribute. As you would imagine, it loops the media playback.
preload
Maybe you're pretty sure that the user wants to activate the media (he's drilled down to it from some navigation, for example, or it's the only reason to be on the page), but you don't want to use autoplay. If so, you can suggest that the browser preload the video so that it begins buffering when the page loads in the expectation that the user will activate the controls.
<video src=leverage-a-synergy.ogv controls preload>
</video>
There are three spec-defined states of the preload attribute. If you just say preload, the user agent can decide what to do. A mobile browser may, for example, default to not preloading until explicitly told to do so by the user.
- preload=auto (or just preload)
A suggestion to the browser that it should begin downloading the entire file. Note that we say "suggestion." The browser may ignore this—perhaps because it detected very slow connection or a setting in a mobile browser "Never preload media" to save the user's bandwidth.
- preload=none
This state suggests to the browser that it shouldn't preload the resource until the user activates the controls.
- preload=metadata
This state suggests to the browser that it should just prefetch metadata (dimensions, first frame, track list, duration, and so on) but that it shouldn't download anything further until the user activates the controls.
src
As on an <img>, this attribute points to the file to be displayed. However, because not all browsers can play the same formats, in production environments you need to have more than one source file. We'll cover this in the next section. Using a single source file with the src attribute is only really useful for rapid prototyping or for intranet sites where you know the user's browser and which codecs it supports.