Home Web Front-end H5 Tutorial HTML5 VideoAPI, build your own Web video player

HTML5 VideoAPI, build your own Web video player

May 15, 2018 am 10:31 AM

1. Basic knowledge

## 1. Usage

<video src="./video/mv.mp4">video>
Copy after login

 

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

  • auto: Tell the browser to download the entire file so that it can be played when the user clicks the play button. Of course, the download process occurs in the background, so web page visitors don't have to wait for the download to complete and can still view the web page as they wish.

  • meta: Tell the browser to get the data block at the beginning of the audio file first, which is enough to determine some basic information (such as the total duration of the audio)

  • none: ** Tells the browser not to download it beforehand. Using these values ​​appropriately can save bandwidth.

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: Explanation

oncanplay : 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: Explanation

play(): 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 w3school

2. Create your own player

We use JavaScript to control playback The behavior of the control (customized playback control) implements the following functions:

  • Use HTML+CSS to create your own playback control bar, and then position it at the bottom of the video

  • Video loading loading effect

  • ##Play, pause

  • Total duration and current playback duration display

  • Playback progress bar

  • Full screen display

## 1.
Playback controls

视频播放器figcaption>

<video src="./video/mv.mp4">video>

a> a>

p>

p>

p> p>

00:00:00span> / 00:00:00span> p> p> p>figure>

The above is all HTML code, the .controls class is the playback control HTML, citing the CSS code:
<link rel="stylesheet" href="./css/font-awesome.css"><link rel="stylesheet" href="./css/player.css">
Copy after login

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;
Copy after login

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");
Copy after login

When the video can be played, display the video

//当视频可播放的时候video.oncanplay = function(){      //显示视频
      this.style.display = "block";      //显示视频总时长
      totalTime.innerHTML = getFormatTime(this.duration);
};
Copy after login

  4.播放、暂停

  点击播放按钮时显示暂停图标,在播放和暂停状态之间切换图标

//播放按钮控制isPlay.onclick = function(){        if(video.paused) {
            video.play();
        } else {
            video.pause();
        }        this.classList.toggle("fa-pause");
};
Copy after login

  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 < 10 ? "0"+h : h;
        m = m < 10 ? "0"+m : m;
        s = s < 10 ? "0"+s : s; 
        return h+":"+m+":"+s;
    }
Copy after login

  6.播放进度条

//播放进度video.ontimeupdate = function(){    var currTime = this.currentTime,    //当前播放时间
    duration = this.duration;       // 视频总时长
    //百分比
    var pre = currTime / duration * 100 + "%";    //显示进度条
    loaded.style.width = pre; 
     //显示当前播放进度时间
    currPlayTime.innerHTML = getFormatTime(currTime);
};
Copy after login

  这样就可以实时显示进度条了,此时,我们还需要点击进度条进行跳跃播放,即我们点击任意时间点视频跳转到当前时间点播放:

//跳跃播放progress.onclick = function(e){    var event = e  window.event;
    video.currentTime = (event.offsetX / this.offsetWidth) * video.duration;
};
Copy after login

  7.全屏显示

  这个功能可以使用HTML5提供的全局API:webkitRequestFullScreen实现,跟video无关:

//全屏expand.onclick = function(){
     video.webkitRequestFullScreen();
};
Copy after login

  经测试在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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles