在网页添加视频

HTML Videos


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" />
  </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.


HTML 4.01 Multimedia Tags

Tag Description
<applet> Deprecated. Defines an embedded applet
<embed> Deprecated in HTML4 but not in HTML5. Defines an embedded object
<object> Defines an embedded object
<param> Defines a parameter for an object

Complete HTML 4.01 Reference.



14,336,250 Sites Built with Wix. Create yours today!

Click here to make a stunning website for Free.

Wix.com offers an easy-to-use online platform that enables anyone to design and publish professional, beautiful websites for free.

With total design control, eCommerce features and superior search engine results, Wix offers a complete solution for launching an exquisite website while saving tons of money.


Award-winning XML Tools: Altova MissionKit

Altova MissionKit

The Altova MissionKit is an integrated suite of tools ideal for:

  • XML development
  • Web & Web services development
  • Data mapping & integration
  • Rendering & publishing XML & database data
  • XBRL validation, taxonomy editing, transformation & rendering
  • Chart & report generation for XML & XBRL

The MissionKit for XML Developers includes XMLSpy® - the industry-leading XML editor; MapForce® - a graphical data mapping, conversion, and integration tool; StyleVision® - a visual XSLT stylesheet and report designer; DiffDog® - an XML-aware diff/merge tool; and 2 additional tools.

Try all 6 products free for 30 days!

Download a fully-functional free trial

  Altova Missionkit

Try it now!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值