Sub topic 1
Embedding audio and video
Prerequisites
This chapter assumes you can read basic HTML structure and attributes, and that you know enough CSS to set a width on an element. Later chapters use JavaScript, but this one does not.
The two elements
The audio and video elements put media in a page without a plugin. Both share the same core behavior: the browser downloads a file, decodes it, and draws a player. They differ in one way. video has a visible picture area, and audio has none, so it shows only a control bar (or nothing at all if you leave controls off).
Both elements belong to the same family, called media elements. They share attributes, events, and a JavaScript API, so what you learn for one applies to the other.
A minimal audio embed
The controls attribute tells the browser to show its built-in player. Without it, the audio element is invisible and silent until a script starts it.
<audio controls src="podcast-episode.mp3">
<a href="podcast-episode.mp3">Download the episode</a>
</audio>The src attribute points at the file. Anything between the tags is fallback content. A browser that supports audio ignores it, and an older browser that does not will show the link instead.
A fuller video embed
Video adds a few more decisions: which formats to offer, how big to draw it, and whether to include captions.
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>Each part has a job:
controlsshows the built-in player.width="640"reserves space so the page does not jump when the video loads.posternames an image to show before playback starts.preload="metadata"fetches only the duration and dimensions, not the whole file.sourceelements list formats in order of preference. The browser uses the first one it can play, judged by thetypeattribute.trackattaches a captions file.- The
pelement is fallback content.
Common errors
- Forgetting
controls. The element renders as empty space, and users cannot start playback. - Putting
srcandsourcechildren together. Ifsrcis present on the element, the browser ignores thesourcechildren. Use one approach. - Omitting
typeonsource. The browser must then download part of each file to find out whether it can play it. Addingtypelets it skip unsupported files without downloading them. - Relying on fallback content for accessibility. Fallback text only appears when the element is unsupported. Captions need a
trackelement, covered in a later chapter.
Exercise
A page has <video controls src="a.mp4"><source src="b.webm" type="video/webm"></video>. Which file does the browser load, and why?
Show answer
It loads a.mp4. When the video element has a src attribute, the browser ignores any source children, so b.webm is never considered.
Exercise
Why should each source element include a type attribute?
Show answer
With type, the browser can tell from the MIME type whether it can play the file and skip it without downloading anything. Without it, the browser has to fetch part of the file to find out.