Skip to main content

Playing Videos in HTML

Videos can be played in HTML by many different methods.

Playing Videos in HTML

Example

<video width="320" height="240" controls="controls">
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  <source src="movie.webm" type="video/webm" />
<object data="movie.mp4" width="320" height="240">
  <embed src="movie.swf" width="320" height="240" />
</object>
</video>

Try it yourself »


Problems, Problems, and Solutions

Displaying videos in HTML is not easy!
You must add a lot of tricks to make sure your video will play in all browsers (Internet Explorer, Chrome, Firefox, Safari, Opera) and on all hardware (PC, Mac , iPad, iPhone).
In this chapter W3Schools summarizes the problems and the solutions.

The <embed> Tag

The purpose of the <embed> tag is to embed multimedia elements in HTML pages.
The following HTML fragment displays a Flash video embedded in a web page:

Example

<embed src="intro.swf" height="200" width="200"/>

Try it yourself »
Problems
  • The <embed> tag is unknown to HTML 4. Your page will not validate correctly.
  • If the browser does not support Flash, your video will not play.
  • iPad and iPhone cannot display Flash videos.
  • If you convert the video to another format, it will still not play in all browsers.

Using The <object> Tag

The purpose of the <object> tag is to embed multimedia elements in HTML pages.
The following HTML fragment displays a Flash video embedded in a web page:

Example

<object data="intro.swf" height="200" width="200"/>

Try it yourself »
Problems:
  • If the browser does not support Flash, your video will not play.
  • iPad and iPhone cannot display Flash videos.
  • If you convert the video to another format, it will still not play in all browsers.

Using the <video> Tag

The <video> element is new in HTML 5.
The purpose of the <video> tag is to embed video elements in HTML pages.
The following HTML fragment displays a video in ogg, mp4, or webm format embedded in a web page:

Example

<video width="320" height="240" controls="controls">
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  <source src="movie.webm" type="video/webm" />
Your browser does not support the video tag.
</video>

Try it yourself »
Problems:
  • You must convert your videos to many different formats.
  • The <video> element does not work in older browsers.
  • The <video> element does not validate in HTML 4 and XHTML.

The Best HTML Solution

HTML 5 + <object> + <embed>

<video width="320" height="240" controls="controls">
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  <source src="movie.webm" type="video/webm" />
<object data="movie.mp4" width="320" height="240">
<embed src="movie.swf" width="320" height="240">
Your browser does not support video
</object>
</video>

Try it yourself »
The example above uses 4 different video formats. The HTML 5 <video> element tries to play the video either in mp4, ogg, or webm formats. If this fails, the code "falls back" to try the <object> element. If this also fails, it "falls back" to the <embed> element.
Problems:
  • You must convert your videos to many different formats.
  • The <video> element does not validate in HTML 4 and XHTML.
  • The <embed> element does not validate in HTML 4 and XHTML.
NOTE: Using <!DOCTYPE html> solves the validation problem.

The YouTube Solution

The easiest way to display videos in HTML is to use YouTube (see next chapter).

Using A Hyperlink

If a web page includes a hyperlink to a media file, most browsers will use a "helper application" to play the file.
The following code fragment displays a link to a Flash video. If a user clicks on the link, the browser will launch a helper application, like Windows Media Player to play the AVI file:

Example

<a href="intro.swf">Play a video file</a>

Try it yourself »


A Note About Inline Videos

When a video is included in a web page it is called inline video.
If you plan to use inline videos in your web applications, be aware that many people find inline videos annoying. Also note that some users might have turned off the inline video option in their browser.
Our best advice is to include inline videos only in web pages where the user expects to see a video. An example of this is a page which opens after the user has clicked on a link to see the video.

Comments

Popular posts from this blog

Toshiba Canada enters all-in-one PC market

Toshiba of Canada has thrown its hat into the all-in-one personal computer (PC) ring with the launch this week of the Toshiba DX730, a 23” all-in-one machine designed for users who want a large display and multimedia features in an environment where space is at a premium. It's Toshiba's first foray into this form factor, said Mini Saluja, national training manager with Toshiba of Canada, building on its experience in the laptop market. The DX730 has a 23” full HD multitouch display with a glossy black finish on an aluminum stand. It comes with a matching Bluetooth keyboard and mouse, and boasts Onkyo stereo speakers with Waves MaxxAudio sound processing. Two models of the DX730 will initially be available. The $899 model features a second-generation Intel Core i3 processor with 4GB of DDR3 memory, a 1TB 7200 RPM hard drive, a DVD SuperMulti Drive and HDMI in. For $1,049, you can move up to a model with an NVIDIA Geforce GT 540M processor and Intel Core i5, as we...

Xiaomi Mi 11 Ultra review

  Introduction Now that the Pro moniker has gone mainstream, it's Ultra that has come to represent the cream of the crop, and the Xiaomi Mi 11 Ultra can wear that badge proudly. Limited to its home market last year, the ultimate Mi has gone global this time around, and we're happy to have it for review today. We're torn whether it's the camera system's physical appearance that is more striking or the hardware inside. A simply massive raised area on the back looks bolted on, almost after the fact, it's hard to miss, and it's a great conversation starter even if it's not everyone's cup of tea. But its size is warranted - the main camera packs the largest sensor used on a modern-day smartphone, and next to it - two more modules unmatched in their own fields, in one way or another. Oh, and yes, there's also a display here - because why not, but also because it can be useful. There's a lot more than 1.1 inches of ...

How to quickly repair your Mobile Phone dropped in water?

Many of you get your mobile phone wet by one way or another. You worry as you mistakenly drop your mobile phone in water. It can also get wet if you are out in a heavy rain.  However, there is no need to panic. It is possible to save your wet mobile phone by quickly repairing it. In order to save your mobile phone from water damage, you can consider these easy and simple solutions: Act rapidly :