- HTML5 is the first new version of the specification since 1999—the Web has changed a lot since then.
- HTML5 will be the new standard for HTML, XHTML and the HTML DOM.
- HTML5 provides a standard way of playing media—a key benefit because there was no standard for playing media in a Web page without a browser plug-in, and no guarantee that every browser would support the plug-in.
- HTML5 is still a work in progress, but most modern browsers have some HTML5 tag support.
To see the difference between using the HTML5 video tag and the traditional object tag to play media, consider the example in Figure 1.
Figure 1 The HTML Video Tag vs. the Object Tag to Play Media
- <section>
- <h1>Using the HTML5 Video tag to play video</h1>
- <video src="video1.mp4" >
- </video>
- </section>
- <section>
- <h1>Using the Object tag to play media using the Flash plug-in</h1>
- <object type="application/x-shockwave-flash"
- data="player.swf" width="290" height="24">
- <param name="movie" value="player.swf">
- </object>
- </section>
Supported Media Formats in HTML5
To use media in your next HTML5 application, you need to know what formats are supported. HTML5 supports AAC, MP3 and Ogg Vorbis for audio and Ogg Theora, WebM and MPEG-4 for video.Even though HTML5 supports these media formats, however, not every browser supports every format. Figure 2 shows current browsers and the media formats they support.
Figure 2 Media Support in Current Browsers
Browser | Video Formats | Audio Formats | ||||
Ogg Theora | H.264 | VP8 (WebM) | Ogg Vorbis | MP3 | Wav | |
Internet Explorer | Manual install | 9.0 | Manual install | No | Yes | No |
Mozilla Firefox | 3.5 | No | 4.0 | Yes | No | Yes |
Google Chrome | 3.0 | No | 6.0 | Yes | Yes | Yes |
Safari | Manual install | 3 | Manual install | No | Yes | Yes |
Opera | 10.50 | No | 10.60 | Yes | No | Yes |
Using the Video Tag
To play a video in an HTML5 page, just use the <video> tag, as shown here:- <video src="video.mp4" controls />
Figure 3 Video Tag Properties
Attribute | Value | Description |
Audio | Muted | Defines the default state of the audio. Currently, only muted is allowed. |
Autoplay | Autoplay | If present, the video starts playing as soon as it’s ready. |
Controls | Controls | Adds Play, Pause and Volume controls. |
Height | Pixels | Sets the height of the video player. |
Loop | Loop | If present, the video will start over again every time it finishes. |
Poster | url | Specifies the URL of an image representing the video. |
Preload | Preload | If present, the video is loaded at page load and is ready to run. It is ignored if Autoplay is present. |
Src | url | The URL of the video to play. |
Width | Pixels | Sets the width of the video player. |
The following code shows a few of the key properties on the video player in a common scenario that includes setting the height and width, autoplay, loop and controls properties, which will display the play, pause and volume controls as well as a fallback error message.
- <video src="video.mp4" width="320" height="240" autoplay controls loop>
- Your browser does not support the video tag.
- </video>
- <!-- H.264 Constrained baseline profile video (main and extended video compatible)
- level 3 and Low-Complexity AAC audio in MP4 container -->
- <source src='video.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
- <!-- H.264 Extended profile video (baseline-compatible) level 3 and Low-Complexity
- AAC audio in MP4 container -->
- <source src='video.mp4' type='video/mp4; codecs="avc1.58A01E, mp4a.40.2"'>
<!-- 3 ways to show the controls -->
<video controls>
<video controls="">
<video controls="controls">
// 2 ways to show controls in JavaScript
video.controls = true;
video.setAttribute
('controls',
'controls');
- <video controls>
- <source src="video1.mp4" />
- <source src="video1.ogv" />
- <source src="video1.webm" />
- <p>This browser does not support HTML5 video</p>
- </video>
- <video controls>
- <source src="video1.mp4" />
- <source src="video1.ogv" />
- <source src="video1.webm" />
- <object data="videoplayer.swf">
- <param name="flashvars" value="video1.mp4">
- HTML5 Video and Flash are not supported
- </object>
- </video>
- var video = document.getElementsByTagName('video')[0];
- if (video.canPlayType)
- { // video tag supported
- if (video.canPlayType('video/ogg; codecs="theora, vorbis"'))
- { // it may be able to play}
- else
- {// the codecs or container are not supported
- fallback(video);
- }
- }
- <video src="video.mp4"
- onerror="fallback(this)">
- video not supported
- </video>
- <video src="video1.mp4" poster="preview.jpg" </video>
Figure 4 Interacting with Video in JavaScript
- var video = document.createElement('video');
- video.src = 'video1.mp4';
- video.controls = true;
- document.body.appendChild(video);
- var video = new Video();
- video.src = 'video1.mp4';
- var video = new Video('video1.mp4')
- <script>
- var video = document.getElementsByTagName('video')[0];
- </script>
- <input type="button" value="Play" onclick="video.play()">
- <input type="button" value="Pause" onclick="video.pause()">
Using the Audio Tag
Using the audio tag is much like using the video tag: you pass one or more audio files to the control, and the first one the browser supports is played.- <audio src="audio.ogg" controls>
- Your browser does not support the audio element.
- </audio>
Figure 5 Audio Tag Properties
Attribute | Value | Description |
Autoplay | autoplay | If present, the audio starts playing as soon as it’s ready. |
Controls | controls | Controls, such as a play button, are displayed. |
Loop | loop | If present, the audio starts playing again (looping) when it reaches the end. |
Preload | preload | If present, the audio is loaded at page load, and is ready to run. It’s ignored if autoplay is present. |
Src | url | The URL of the audio to play. |
As with the video tag, you can pass multiple files to the audio tag and the first one that is supported will play. You can also use a fallback message when the browser doesn’t support the audio tag, like this:
- <audio controls autoplay>
- <source src="audio1.ogg" type="audio/ogg" />
- <source src="audio1.mp3" type="audio/mpeg" />
- Your browser does not support the audio element.
- </audio>
Summary
HTML5 is the next standard for the Web, and depending on the browsers you’re targeting, you can start using some of the new tags, such as audio and video, right now. Be cautious when using HTML5, however, because not every browser supports the new tags, and even if one does, it might not support every media format. If you’re using a modern browser that does support HTML5, you still need to do the extra work to process your media in all formats to ensure user success. Here are some great Web resources that provide browser support information as well as all the other information you need to ensure HTML5 media success:- http://www.w3.org/TR/html5/video.html
- http://dev.w3.org/html5/spec/Overview.html
- http://w3schools.com/html5/default.asp
- http://html5test.com/
- http://caniuse.com/
No comments:
Post a Comment
Note: only a member of this blog may post a comment.