How to play video with HTML5

thumbnailPlacing a video in HTML5 markup is simple, no more complex for any given browser than placing an image. In this article we’ll take full advantage of the built-in browser support to build the simplest possible video player.

We’ll lay the application’s basic framework and then use the <video> element to add a video to the web page without the need of any plugins at all.

Prerequisites

Use Chrome, Safari or Internet Explorer 9+. For the time-being you’ll have to avoid Firefox and Opera because of the cross-browser video file format issues. Although support for the video element is consistent across all modern browsers, the MP4 format trips up Firefox and Opera. You can check for compatibility here.

Before you begin you’ll need to find a .mp4 that you can use, if you don’t have one, you’ll find lots of free mp4 files online.

Building the basic framework

The following code is the framework around which you build the player. It creates a simple layout and has a placeholder for the video itself.

You need to create a new HTML file in your working directory and name it index.html, then add this code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Video Player</title>
<style>
body {
font-family: sans-serif;
border: 0;
margin: 0;
padding: 0;
}
header {
text-align: center;
}
#player {
display: table;
width: 100%;
padding: 4px;
}
#player > div {
display: table-cell;
vertical-align: top;
}
</style>
</head>
<body>
<header>
<h1>HTML5 Video Player</h1>
</header>
<section id="player">
<div>
<!-- The video will appear here-->
</div>
</section>
</body>
</html>

Now, with the foundation laid, let’s get to the fun part of the player by adding a video to the page.

Using the video element to add videos to web pages

The goal in designing HTML5’s <video> element was to make the embedding of video within a web page as straightforward as embedding an image. Although you’ll encounter additional complexities because video file formats are more feature-rich than image formats, the design goal has been attained.

Here’s what an HTML5 video looks like in Chrome:

html5_action_002

The next listing shows all of the code required to display the video. As you can see, it’s not complicated.

Insert this code in place of the “<!– The video will appear here–>” comment in the code above, make sure you replace [ YOUR VIDEO ] with the path to your .mp4, and refresh the page.

<video src="[ YOUR VIDEO ].mp4"
controls
width="720" height="480">
Your browser does not support the video element, please #D try <a href="videos/VID_20120122_133036.mp4">downloading the video instead</a>
</video>

In this code the src attribute is the path and file name of your .mp4 file, the controls attribute indicates that you would like the video to have the standard controls (omit it if you don’t want the controls) and the width and height are self-explanatory. The text inside the element is back up text to be displayed if the video tag isn’t recognized by the browser.

Summary

The web is increasingly being used as a replacement for traditional broadcast media. Services like Netflix, YouTube, Spotify, last.fm, and Google Music seek to replace your DVD and CD collections with online players. With HTML5, video and audio become first-class citizens of web content. Rather than handing responsibility for playing media to a third-party application, it’s played within the browser, allowing you to control and manipulate media from within your web application.

As you can see HTML5 makes it as straightforward a process to add video and audio to web pages as it is to add images.

How do you prefer to handle video? Is HTML5’s <video> element robust enough? Let us know your thoughts in the comments.

Featured image/thumbnail, video image via Shutterstock.

WDD Staff

WDD Staff

WDD staff are proud to be able to bring you this daily blog about web design and development. If there's something you think we should be talking about let us know @DesignerDepot.

Join to our thriving community of like-minded creatives!