HTML5 Video

本文介绍了HTML5中用于展示视频的标准与元素,探讨了不同浏览器对于视频格式的支持情况,并提供了使用HTML5视频标签的基本示例及如何通过JavaScript操控视频播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.w3schools.com/html/html5_video.asp

Many modern websites show videos. HTML5 provides a standard for showing them.

Video on the Web

Before HTML5, there was no standard for showing videos/movies on web pages.

Before HTML5, videos could only be played with a plug-in (like flash). However, different browsers supported different plug-ins.

HTML5 defines a new element which specifies a standard way to embed a video or movie on a web page: the <video> element.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the <video> element.

Note: Internet Explorer 8 and earlier versions, do not support the <video> element.


HTML5 Video - How It Works

To show a video in HTML5, this is all you need:

<!DOCTYPE html>
<html>
<body>

<video width="320" height="240" controls>
  <source src="movie.mp4" type="http://www.w3schools.com/html/video/mp4">
  <source src="movie.ogg" type="http://www.w3schools.com/html/video/ogg">
  Your browser does not support the video tag.
</video>

</body>
</html>

The control attribute adds video controls, like play, pause, and volume.

It is also a good idea to always include width and height attributes. If height and width are set, the space required for the video is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the video, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the video loads).

You should also insert text content between the <video> and </video> tags for browsers that do not support the <video> element.

The <video> element allows multiple <source> elements. <source> elements can link to different video files. The browser will use the first recognized format.

Video Formats and Browser Support

Currently, there are 3 supported video formats for the <video> element: MP4, WebM, and Ogg:

Browser MP4 WebM Ogg
Internet Explorer YES NO NO
Chrome YES YES YES
Firefox NO
Update: Firefox 21 running on Windows 7, Windows 8, Windows Vista, and Android now supports MP4
YES YES
Safari YES NO NO
Opera NO YES YES
  • MP4 = MPEG 4 files with H264 video codec and AAC audio codec
  • WebM = WebM files with VP8 video codec and Vorbis audio codec
  • Ogg = Ogg files with Theora video codec and Vorbis audio codec

MIME Types for Video Formats

Format MIME-type
MP4 video/mp4
WebM video/webm
Ogg video/ogg


HTML5 <video> - DOM Methods and Properties

HTML5 has DOM methods, properties, and events for the <video> and <audio> elements.

These methods, properties, and events allow you to manipulate <video> and <audio> elements using JavaScript.

There are methods for playing, pausing, and loading, for example and there are properties (like duration and volume). There are also DOM events that can notify you when the <video> element begins to play, is paused, is ended, etc.

The example below illustrate, in a simple way, how to address a <video> element, read and set properties, and call methods.

Example 1

Create simple play/pause + resize controls for a video:


The example above calls two methods: play() and pause(). It also uses two properties: paused and width.

<!DOCTYPE html> 
<html> 
<body> 

<div style="text-align:center"> 
  <button onclick="playPause()">Play/Pause</button> 
  <button onclick="makeBig()">Big</button>
  <button onclick="makeSmall()">Small</button>
  <button onclick="makeNormal()">Normal</button>
  <br> 
  <video id="video1" width="420">
    <source src="mov_bbb.mp4" type="video/mp4">
    <source src="mov_bbb.ogg" type="video/ogg">
    Your browser does not support HTML5 video.
  </video>
</div> 

<script> 
var myVideo=document.getElementById("video1"); 

function playPause()
{ 
if (myVideo.paused) 
  myVideo.play(); 
else 
  myVideo.pause(); 
} 

function makeBig()
{ 
myVideo.width=560; 
} 

function makeSmall()
{ 
myVideo.width=320; 
} 

function makeNormal()
{ 
myVideo.width=420; 
} 
</script> 

<p>Video courtesy of <a href="http://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>
</body> 
</html>


For a full reference go to our  HTML5 Audio/Video DOM Reference .


HTML5 Video Tags

Tag Description
<video> Defines a video or movie
<source> Defines multiple media resources for media elements, such as <video> and <audio>
<track> Defines text tracks in media players

### HTML5 Video 标签的用法与实例 HTML5 的 `<video>` 标签允许网页内嵌视频播放器而无需依赖第三方插件。此功能使得创建多媒体丰富的网站变得更加简单和高效[^1]。 #### 基本语法结构 最简单的形式如下所示: ```html <video> Your browser does not support the video tag. </video> ``` 为了使浏览器能够显示视频并提供基本控制按钮(如播放、暂停),可以添加 `controls` 属性以及指定视频文件的位置: ```html <video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> Your browser does not support the video tag. </video> ``` 这里定义了一个宽度为320像素,高度为240像素的视频区域,并指定了一个MP4格式的源文件。对于不支持该标签的老版本浏览器,则会显示出提示信息:“Your browser does not support the video tag.” #### 多种编码格式的支持 考虑到不同浏览器可能对特定类型的媒体编解码有不同的兼容情况,在实际应用中通常建议同时提供多种格式来确保跨平台一致性: ```html <video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> <!-- For Safari --> <source src="movie.ogg" type="video/ogg"> <!-- For Firefox, Chrome, Opera --> <source src="movie.webm" type="video/webm"><!-- For Chrome, Firefox, Opera --> Your browser does not support the video tag. </video> ``` 通过这种方式,即使某些用户的设备无法处理一种特定格式,也有可能找到另一种可接受的选择来进行正常回放。 #### 自动播放与循环设置 有时希望页面加载完成后立即自动开始播放视频,或是让视频结束后重新从头再播一次,这时可以通过增加相应的属性实现这些需求: ```html <video autoplay loop muted> <source src="demo_video.mp4" type="video/mp4"> Fallback message for unsupported browsers. </video> ``` 注意:由于许多现代移动操作系统出于节省电量考虑,默认情况下不允许静音以外任何形式的自动化音频输出,因此当设置了 `autoplay` 同时常需配合使用 `muted` 来保证最佳用户体验。 #### 使用JavaScript增强互动体验 除了静态配置外,还可以借助于JavaScript动态调整视频的行为逻辑,比如监听事件触发自定义操作等: ```javascript const myVideo = document.querySelector('video'); myVideo.addEventListener('ended', function() { console.log("The video has ended!"); }); ``` 上述代码片段展示了如何捕捉到视频结束这一时刻并向开发者工具的日志窗口打印一条消息通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值