HTML5 Development Class: Media Events

By Curtis Dicken

http://www.htmlgoodies.com/html5/tutorials/HTML5-Development-Class-Media-Events-3883356.htm (Back to article)

Introduction

There has been much discussion surrounding the Media capabilities in HTML5. How will it impact Flash? Will we still need different media players like QuickTime and Windows Media Player? Why is HTML5 attempting to standardize media on the web?

While all of these are valid discussions that will be bantered about for the foreseeable future, this article will only provide you a foundation for the media events of HTML5 and a brief explanation of how media elements work. It’s up to you to decide what side of the different controversies you want to be on. Hopefully this article will give you the foundation for making informed opinions on HTML5 and its media capabilities.

About Media Events

Unlike other areas of HTML like mouse and keyboard events where there are both new HTML5 events and events ported from HTML4 to HTML5, media events in HTML5 are all new. All developers will have to learn about how media elements, attributes and events work with HTML5.

For new developers, once you have mastered the basic concepts of HTML elements, attributes and events you should be able to understand and begin using media with HTML5 fairly quickly. Seasoned developers should appreciate the straight-forward and intuitive design of HTML5’s media capabilities.

Now, before we get into the events you should first understand how a media element is structured. In this article I will use audio as my example but many of the events also apply to video, embed, img and object elements as well. However, it’s with the use of audio and video that these events will find their most frequent use.

Note: Currently audio and video file type support is extremely limited. The only file types currently included in the HTML5 specs at this time are ogg (audio), mp3 (audio), wav (audio), ogg (video) and mp4 (video). It should also be noted that browser implementation of the media capabilities is sketchy at best ranging to virtually no implementation in IE8 to almost complete implementation in Google Chrome 3.0.

Below is an example of what an audio element may look like. Make special note of the source element within the audio element and the controls attribute. With regard to the controls attribute, keep in mind some of the events listed in this article will never fire when the controls are not displayed to the user.

<audio controls="controls">
 <source src="HtmlGoodiesThemeSong.ogg" type="audio/ogg">
 <source src="HtmlGoodiesThemeSong.mp3" type="audio/mpeg">
Sorry, your browser does not support the audio element.
</audio>

The brief functional breakdown of the element above would be:

  1. Try to play the .ogg audio file
  2. If item 1 fails, try to play the .mp3 file
  3. If item 2 fails, display the “browser does not support” message

If a browser does not understand the audio tag at all it should simply ignore the tag and display the “browser does not support” message. This “default message” concept is built into most of the new HTML5 elements for the purposes of backwards compatibility.

Media Events Explored

The first thing you will notice when starting to work with the media capabilities in HTML5 is that the same events apply to different types of standard media. Whether you are offering video or audio the events are the same. The second thing that you will notice is comprehensiveness of the events. The events cover all the most common points where a developer would want to perform an action.

Conclusion

As you can see by the lengthy list above there are events at just about every point where a developer would want to perform some action. Most are intuitive and some overlap others. Even if you are violently opposed to the media functionality in HTML5, you must admit that it does make it very easy to add media to your web pages with very robust events for handling the user and browser interaction with the media.

Remember, though, implementation of the HTML5 specification is still very inconsistent among browsers so I would suggest testing out your scripts in Google Chrome as it has the most complete implementation so far. Happy coding!