H5打造属于自己的视频播放器(JS篇1)

2021-03-28 16:25

阅读:654

回顾

1)H5打造属于自己的视频播放器(HTML篇)
2)H5打造属于自己的视频播放器(逻辑篇)

前言

在HTML篇当中我们写好了样式,今天我们为video新增一个样式

.bad-video video {
    width: 100%;
    height: 100%;
    display: block;
    object-fit:fill;
}

高,宽,内容,都充父元素

乳此一来,我们的HMTL元素内容为

        
00:00/00:00
全屏

吹着口哨写JS

幸好我们在写JS之前,先整理好了逻辑思绪,并写好了笔记H5打造属于自己的视频播放器(逻辑篇)

以下文章将要用到 号称 最接近原生APP体验的高性能前端框架MUI

首先,让我们来新建一个js文件,比如,bvd.js 并注册一下全局的一个入口

(function($){
    var bvd = function(dom) {
        this.dom = document.querySelector(dom || ‘video‘);
    }
    
    var pro = bvd.prototype;
    
    pro.test = function(){
        console.log(this.dom)
    }
    
    
    var nv = null;
    $.bvd = function(dom) {
        return nv || (nv = new bvd(dom));
    }
}(mui))

让我们在video.html引入bvd.js 并且来测试一下吧!

技术图片

看来是ok的,与此同时,为了html页的简洁美观,我们将播放按钮控制条用JS动态生成

(function($){
    var bvd = function(dom) {
        var that = this;
        $.ready(function(){
            //获取视频元素
            that.video = document.querySelector(dom || ‘video‘);
            //获取视频父元素
            that.vRoom = that.video.parentNode;
            //元素初始化
            that.initEm();
        })
    }
    
    var pro = bvd.prototype;
    
    pro.initEm = function(){
        //先添加播放按钮
        this.vimg = document.createElement("img");
        this.vimg.src = ‘img/play.png‘;
        this.vimg.className = ‘vplay‘;
        this.vRoom.appendChild(this.vimg);
        
    }
    
    
    var nv = null;
    $.bvd = function(dom) {
        return nv || (nv = new bvd(dom));
    }
}(mui))

这里有个小细节,如果img的src设置为本地资源的话,那么以后使用会出现很多问题,比如,页面层级发生变化时,你要去修改bvd.js,以免夜长梦多,我们将图片转为base64

再添加控制条

pro.initEm = function(){
        //先添加播放按钮
        this.vimg = document.createElement("img");
        this.vimg.src = ‘data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIA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3wj8RjOzbs/IrXAnUzaEqqM9slh2Bs2yJN83NXi37SGQgGQBwRxtK91KLVkk9q9Aw4ItjHxMhicNWZ8rDu/5sK7XySmm7uXNpfCAZAHBFG0D9Cq1n6iGkwWcG+Dx+DN07abCH98OntJQAOhvo/EvouvnwXEUgCQBwTRJHO+zJXo0AMiegyIY18reSRxE1NytiuVOGUy38yhNAAsQBBoNbQz4KKcAm9f5pIybrndIvEFEfqW/Quz9itVAesfk4R51/s8lLDv8ensBQENpeJ+iQ6lM790CbDM1PWkUOlOMPYBAA6b9LJ3nLWrXuaqS1Rx4clSVcA6kxNXbpTzCdcECHvvfI32GcNU6b9e5GJ84uProNWTekTSTVSLEZj2Uce3eOJhicwB1ChU+rbuiF+ynueG6VKXLoU2tdQrqFQCLC88SirB6J5Arpe978YiujGWf6iCD1XtnCQ1+1sg6to8IrGR8TVXI6sNAHeuolZ9BHhVhDhLDoXGiDDRpCYUzD+6SgizwzFsfaNQd4igE6WwAxbyGIQPxSdrBcCC6ZHuDP80toEozGsNFqIxAU4BYbrcFyH+NwGsPgpB0Eagto2/bjMnk7rtS/FJLwCwIJ40zGtDbJ/aGsYrCgY+JbJ4BYCHCgQfGb+gtZcAWAYCKW2iZ8992skWc7lFrYY+7fj1uXsNgC82QnSHsAdAvTwxBAuGOTc1sXsANK+Jj1wNUOfJCAZoBACW15G4YD0gOnB2IQUEEjUxrhziTaj2hr4GqdLW0TgArILBeA9/dxGpm/jkbjEFEbfj0rIm9kCEYx18NW4a09c8Iemq/W8X5QjM/+6Qog4CtImo7epaGgMOEacEMEWNk7D11aTJDG+kDeAKvUVhiuVxNBo/n2L/n3CqCFdiBPPWfNIEHe5Km0arANfF7/qXnBCyI7D/FNhJAP95VOoM/w/9eiom7K02egAAAABJRU5ErkJggg==‘;
        this.vimg.className = ‘vplay‘;
        this.vRoom.appendChild(this.vimg);
        
        //添加控制条
        this.vC = document.createElement("div");
        this.vC.classList.add(‘controls‘);
        this.vC.innerHTML = ‘
00:00/00:00
全屏
‘; this.vRoom.appendChild(this.vC); }

再把 video.html中的 播放按钮 和 控制条 注释,看看我们写的代码效果如何

技术图片
大家可以看到,红色是我们注释的,绿色的是我们js生成的,奈斯

开始播放

真正开始我们js篇的第一步啦

初始化事件,先写个播放按钮播放视频事件测试一下

            //元素初始化
            that.initEm();
            //事件初始化
            that.initEvent();

----------

    pro.initEvent = function(){
        var that = this;
        //给播放按钮图片添加事件
        this.vimg.addEventListener(‘tap‘,function(){
            that.video.play();
        })
    }

tap事件,是MUI封装好的一个适合移动端点击的事件哦,移动端click事件延迟300ms,建议大家多用tap,关于tap事件是如何模拟的,请看HTML5触摸事件演化tap事件介绍

哟西,视频开始播放拉!!!

技术图片

回顾一下H5打造属于自己的视频播放器(逻辑篇)
温习一下移动端HTML5

初始化

1)封面 在这里我没啥好图,就不设置了,大家可以试试在video标签加入属性 poster="图片地址"
2)获取视频长度,ok,我们给视频添加一个"loadedmetadata"获取到元数据事件,什么事元数据?

举个栗子:一部电影30个G,名字叫《我将带头冲锋》,视频长300小时

大小,名称,长度,等这些应该就算是元数据了。

我们给视频添加一个获取到元数据事件

    pro.initEvent = function(){
        var that = this;
        //给播放按钮图片添加事件
        this.vimg.addEventListener(‘tap‘,function(){
            this.style.display = ‘none‘;
            that.video.play();
        })
        
        //获取到元数据
        this.video.addEventListener(‘loadedmetadata‘,function(){
            that.vC.querySelector(‘.duration‘).innerHTML = this.duration;
        });
    }

技术图片

果然出现了视频的长度,可是这是按秒计算的啊。。。

这时我们可以写一个时间转换的函数

    function stom(t) {
        var m = Math.floor(t / 60);
        m 

转换一下

技术图片

可以,这很清真,今天我们暂时讲到这,消化消息,也让楼主整顿一下思绪

本章节JS1源码放在这里

H5打造属于自己的视频播放器(JS篇2)


评论


亲,登录后才可以留言!