1. Basic knowledge ## 1. Usage <video src="./video/mv.mp4">video> Note: The audio and video elements must contain both start and end tags, and cannot be used 2. Important HTML attributes Controls: ontrol: If this attribute appears, controls are displayed to the user, such as a play button. The playback controls in each browser are different, but the purpose is the same. You can control the start and end, jump to a new position and adjust the volume Autoplay: autoplay: If it appears With this attribute, the video will be played as soon as it is ready. If the autoplay attribute is not set, the audio file must be played when the user clicks the play button. loop:loop: (loop playback) tells the browser to start playing again from the beginning when the audio reaches the end preload:auto, mete, none: Tell the browser how to download the audio If the preload attribute is not set, the browser will decide whether to pre-download it. Different browsers handle this differently. Most browsers use auto as the default, but Firefox's default is metadata. However, please also note that this preload attribute is not a rule that must be strictly implemented, but is just a suggestion for the browser. Depending on the circumstances, the browser may ignore your settings. (Some older browsers will not care about the preload attribute.) 3. Common events Event name: Explanationoncanplay : Script to run when the file is ready to start playing (when buffering is sufficient to start). ontimeupdate: A script that runs when the playback position changes (such as when the user fast-forwards to a different position in the media). onended: A script that runs when the media has reached the end (can send a message like "Thanks for watching"). 4. Commonly used methods Method name: Explanationplay(): Start playing audio/video pause():Pause the currently playing audio/video 5. Commonly used API attributes Attribute name: Explanation
duration: Returns the length of the current audio/video (in seconds) paused: Sets or returns whether the audio/video is paused currentTime: Set or return the current playback position in the audio/video (in seconds) ended: Return whether the playback of the audio/video has ended For more attributes, events, and methods, please view w3school2. Create your own player We use JavaScript to control playback The behavior of the control (customized playback control) implements the following functions:
<figure> <figcaption>视频播放器figcaption> <p> <video>video> <p> <a>a> </a><a>a> <p> </p> <p>p> </p> <p>p> </p> <p>p> p> </p> <p> <span>00:00:00span> / <span>00:00:00span> p> p> p>figure></span></span></p></a></p></video></p></figcaption></figure>The above is all HTML code, the .controls class is the playback control HTML, citing the CSS code: <link><link>In order to display the play button and other icons, I used the font icon 2. Video loading loading effect Hide the video at the beginning, replace it with a background image, and then display the video when the video is loaded and ready to be played CSS:.player { width: 720px; height: 360px; margin: 0 auto; background: #000 url(../images/loading.gif) center/300px no-repeat; position: relative;} video { display: none; height: 100%; margin: 0 auto; 3. Play Function Let’s start writing javascript code. First we get the DOM elements to be used: var video = document.querySelector("video");var isPlay = document.querySelector(".switch");var expand = document.querySelector(".expand");var progress = document.querySelector(".progress");var loaded = document.querySelector(".progress > .loaded");var currPlayTime = document.querySelector(".timer > .current");var totalTime = document.querySelector(".timer > .total");When the video can be played, display the video //当视频可播放的时候video.oncanplay = function(){ //显示视频 this.style.display = "block"; //显示视频总时长 totalTime.innerHTML = getFormatTime(this.duration); }; 4.播放、暂停 点击播放按钮时显示暂停图标,在播放和暂停状态之间切换图标 //播放按钮控制isPlay.onclick = function(){ if(video.paused) { video.play(); } else { video.pause(); } this.classList.toggle("fa-pause"); }; 5.总时长和当前播放时长显示 前面代码中其实已经设置了相关代码,此时我们只需要把获取到的毫秒数转换成我们需要的时间格式即可,提供getFormatTime()函数: function getFormatTime(time) { var time = time 0; var h = parseInt(time/3600), m = parseInt(time%3600/60), s = parseInt(time%60); h = h <p><strong> 6.播放进度条</strong></p><pre class='brush:php;toolbar:false;'>//播放进度video.ontimeupdate = function(){ var currTime = this.currentTime, //当前播放时间 duration = this.duration; // 视频总时长 //百分比 var pre = currTime / duration * 100 + "%"; //显示进度条 loaded.style.width = pre; //显示当前播放进度时间 currPlayTime.innerHTML = getFormatTime(currTime); }; 这样就可以实时显示进度条了,此时,我们还需要点击进度条进行跳跃播放,即我们点击任意时间点视频跳转到当前时间点播放: //跳跃播放progress.onclick = function(e){ var event = e window.event; video.currentTime = (event.offsetX / this.offsetWidth) * video.duration; }; 7.全屏显示 这个功能可以使用HTML5提供的全局API:webkitRequestFullScreen实现,跟video无关: //全屏expand.onclick = function(){ video.webkitRequestFullScreen(); }; 经测试在firefox、IE下全屏功能不可用,这样正常了,全屏API是针对webkit内核的。 |
The above is the detailed content of HTML5 VideoAPI, build your own Web video player. For more information, please follow other related articles on the PHP Chinese website!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。

因为html5不基于SGML(标准通用置标语言),不需要对DTD进行引用,但是需要doctype来规范浏览器的行为,也即按照正常的方式来运行,因此html5只需要写doctype即可。“!DOCTYPE”是一种标准通用标记语言的文档类型声明,用于告诉浏览器编写页面所用的标记的版本。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft