HTML5 Media
Problem Statement
The internet is a highly interactive environment. As HTML authors, we might be given a media file and be told to put it on the internet. How can we display media inside of a web page and make sure that it's viewable to the most people possible on the most devices? That will be the focus of this lesson.
Overview
Explain the history of media on the web
Demonstrate how to embed audio elements in HTML5
Demonstrate how to embed video elements in HTML5
Link to audio and video converters
Explain The History Of Media On The Web
In the early days of HTML, media elements were more difficult to use. They would often require the user to download and install a plugin. Media plugins you might recall were Macromedia Flash, Adobe Shockwave, and Java. This commercial plugin approach brings about a number of problems.
It is seriously annoying to have to download all these plugins
The site is unusable while you wait for a plugin to load
The site is unusable without the plugin
The site is unusable for those using assistive devices
Bad guys could market a viral site which required the installation of a plugin. Plugin installation gives access to the operating system where they could install malware
If a company's plugin became dominant, there would be a splitting of the web into a commercial thing versus the non-commercial thing ("The Internet, powered by Adobe Flash")
For these reasons and others, the W3C added media support in HTML5.
Demonstrate How to Embed Audio Elements in HTML5
To include audio in a website, use the <audio>
element. Inside the element, we provide <source>
elements whose src
attributes point to a file on the server and whose type
attributes specify what type of media it is.
You might recall that files fit into two big buckets: binary and text. Sometimes we need to be more specific within those groups. We want to say this is a text file, but also HTML (text/html
). Or we want to say this is a binary file, but also an MPEG movie (video/mpeg
). A MIME type is a way to note, for the computer, exactly what type of file is present. It will help the computer find the right player. It's also a bit more precise than a simple file extension (.docx
or .img
).
To pull it all together, the type
attribute should be set to the "MIME type" for the media pointed to by the src
attribute for each <source>
element.
Let's look at an example:
In lines two and three, we provide two different source files for playback. If the browser does not recognize the first file type, it will ignore it and move on to the next. If neither of the formats are supported it will instead display the paragraph on line four. If the browser is able to play one of the source files it will ignore any other code below until it reaches the closing </audio>
tag.
Demonstrate How To Embed Video Elements in HTML5
Embedding a video is very similar to embedding audio. This can be done by including the <video>
tag. Inside the video tag are source tags that point to the location of various video file formats and specify their MIME types.
On lines two and three we provide two different source files for playback. If the browser does not recognize the first filetype it will ignore it and move on to the next just the same as it does for the audio element. If neither of the formats are supported it will display the paragraph instead on line four. If the browser is able to play one of the source files, it will. The others sources within the <audio>
tag will be ignored.
Link to Audio and Video Converters
Conclusion
With audio
and video
tags, the W3C gives us an open way to ensure that media remain accessible and open to all platforms.
Last updated