HTML5自定视頻播发器源代码

2021-02-22 11:21 jianzhan

video目标

适配性写法

    <video controls>
        <source src="data/demo.ovg">
        <source src="data/demo.mp4">
        <source src="data/demo.webm">
        您的访问器不适用,请升級您的访问器
    </video>

video 标识 width height autoplay muted

poster带有预览图(海报照片)的视頻播发器

<video src='data/demo.mp4' muted controls autoplay height='400' width="200" poster='data/poster.jpg'></video>

选定video标识

    var VideoNode = document.getElementById('myVideo');

src操纵视頻的来源于

VideoNode.src = 'data/demo.ogv';

手动式设定控制 controls

VideoNode.controls = true;

设定视頻声音

VideoNode.volume = 0.5;

currentTime当今播发時间

快进实际效果

gogogo.onclick = function(){
            VideoNode.currentTime = VideoNode.currentTime + 3;
        };

中止 pause()

      stopNode.onclick = function(){
            VideoNode.pause();
        };

播发play()

     playNode.onclick = function(){
            VideoNode.play();
        };

load 更新播发器的恶性事件

      reloadNode.onclick = function(){
            VideoNode.src = 'data/demo.mp4';
            VideoNode.load();
        };

canplay 视頻早已载入好 能够刚开始播发了

 VideoNode.addEventListener('canplay',function(){
            console.log('视頻早已载入好 能够刚开始播发了');
        });

requestFullscreen 让video标识变为全屏

VideoNode.webkitRequestFullscreen();
VideoNode.mozRequestFullScreen();

        fullScreenNode.onclick = function(){
            if(VideoNode.webkitRequestFullscreen){
                VideoNode.webkitRequestFullscreen();
            }
            else if(VideoNode.mozRequestFullScreen){
                VideoNode.mozRequestFullScreen();
            }
        };

volumechange 当声音变更时

    VideoNode.onvolumechange = function(){
            console.log('volumechange');
        };

响声任意变更

        volumeNode.onclick = function(){
            VideoNode.volume = Math.random();
        };

seeking 当客户刚开始拖拽进度条时 就会开启的恶性事件

 var seekingNum = 0;
        VideoNode.onseeking = function(){
            console.log('seeking...');
            seekingNum++;
            seeking.innerHTML = seekingNum;
        };

seeked 当客户对视頻的进度条而且早已进行实际操作时会开启的恶性事件

   var seekedNum = 0;
        VideoNode.onseeked = function(){
            console.log('seeked...');
            seekedNum++;
            seeked.innerHTML = seekedNum;
        };

timeupdate监视视頻播发的情况

       VideoNode.addEventListener('timeupdate',function(){
            // 总时长,以分钟:秒的方式显示信息
            let allTime = parseInt(VideoNode.duration/60)+':'+parseInt(VideoNode.duration%60);
            // 当今時间,以分钟:秒的方式显示信息
            let nowTime = parseInt(VideoNode.currentTime/60)+':'+parseInt(VideoNode.currentTime%60);
            timeNode.innerHTML = nowTime+'/'+allTime;
        })

readyState 视頻的提前准备信息内容

  console.log(VideoNode.readyState);
        setTimeout(function(){
            console.log(VideoNode.readyState);
        },500);

playbackRate 查询或设定视頻的1个播发速率

 console.log(VideoNode.playbackRate)

Rate设定倍速

//Rate设定0.5倍速
        RateNode.children[0].onclick = function(){
            VideoNode.playbackRate = 0.5;
        };
        //Rate设定1倍速
        RateNode.children[1].onclick = function(){
            VideoNode.playbackRate = 1;
        };
        //Rate设定2倍速
        RateNode.children[2].onclick = function(){
            VideoNode.playbackRate = 2;
        };

loop的设定

 loopNode.onclick = function(){
            if(VideoNode.loop == false){
                this.innerHTML = '循环系统';
                VideoNode.loop = true;
            }
            else{
                this.innerHTML = '不循环系统';
                VideoNode.loop = false;
            }
        };

src回到的数据信息

console.log('src='+VideoNode.src);

currentSrc回到的数据信息

console.log('currentSrc='+VideoNode.currentSrc);

监视ended恶性事件

VideoNode.addEventListener('ended',function(){
            console.log('视頻播发完毕了');
            VideoNode.play();
        })

查询视頻的互联网情况

console.log(VideoNode.networkState)

手动式设定控制 controls

VideoNode.controls = true;

手动式设定静音 muted

VideoNode.muted = true;

自定视頻播发器

放图

<!doctype html> 
<html> 
<head> 
    <meta charset="utf⑻"> 
    <title></title> 
    <style type="text/css">
*{margin: 0;padding: 0;list-style: none;} 
.outerNode{width: 540px;height: 332px;position: absolute;left: 50%;top: 50%;margin: ⑴66px 0 0 ⑵70px;box-shadow: 0 0 4px #5b606d;}  
.outerNode .videoNode{
    width: 540px;height: 305px;float: left;
    background: black;
}     
.outerNode .controlsNode{
    width: 540px;height: 27px;float: left;background: url(images/ctrs_bg.gif) repeat-x;
}
.outerNode .controlsNode .playNode{
    float: left;width: 15px;height: 17px;margin: 6px 0 0 14px;
    background: url(images/playNode.png) no-repeat;cursor: pointer;
}
.outerNode .controlsNode .pauseNode{
    float: left;width: 15px;height: 17px;margin: 6px 0 0 14px;
    background: url(images/pause.png) no-repeat;cursor: pointer;
}
.outerNode .controlsNode .loadNode{width: 267px;height: 10px;margin: 9px 0 0 14px;float: left;background: url(images/load_bg.png) repeat-x;position: relative;}
.outerNode .controlsNode .loadNode .lineNode{
    width: 0%;height: 7px;position: absolute;left: 0;top: 1px;
    background: url(images/line_bg.png) repeat-x;
}
.outerNode .controlsNode .loadNode .lineNode .lineRight{
    width: 2px;height: 100%;position: absolute;right: 0;top: 0;
    background: url(images/line_r_bg.png) no-repeat;
}
.outerNode .controlsNode .loadNode .loadLeft{
    height: 100%;width:3px ;position: absolute;left: 0;top: 0;
    background: url(images/left_bg.png) no-repeat;z-index: 4;
}
.outerNode .controlsNode .loadNode .loadRight{
    height: 100%;width:3px ;position: absolute;right: 0;top: 0;
    background: url(images/right_bg.png) no-repeat;
}
.outerNode .controlsNode .loadNode .crlNode{width: 17px;height: 17px;background: url(images/crl_bg.png);position: absolute;left: ⑻.5px;top: ⑶.5px;cursor: pointer;z-index: 5;}
.outerNode .controlsNode .timeNode{
    float: left;width: 75px;height: 10px;margin: 9px 0 0 9px;
}
.outerNode .controlsNode .timeNode span{font-size:10px;float: left;line-height: 10px;color: white; }
.outerNode .controlsNode .volumeNode{
    width: 17px;height: 16px;float: left;margin: 5px 0 0 6px;
    background: url(images/volume_bg.png);
}
.outerNode .controlsNode .volumeLine{
    height: 8px;width: 61px;float: left;margin: 10px 0 0 4px;
    background: url(images/volumeLine_bg.png) repeat-x;position: relative; 
}
.outerNode .controlsNode .volumeLine .v_left{
    width: 3px;height:100%;position: absolute;left: 0;top: 0;background: url(images/v_left.png) no-repeat;
}
.outerNode .controlsNode .volumeLine .v_right{
    width: 3px;height:100%;position: absolute;right: 0;top: 0;background: url(images/v_right.png) no-repeat;
}
.outerNode .controlsNode .volumeLine .v_DragNode{width: 15px;height: 13px;position: absolute;left: 58.5px;top: ⑶.5px;background: url(images/vo_d.png) no-repeat;cursor: pointer;}
.outerNode .controlsNode .fullNode{
    width:15px;height:17px;float: left;margin: 6px 0 0 35px;
    background: url(images/full_bg.png) no-repeat;cursor: pointer;
    transition: 0.7s;
}
.outerNode .controlsNode .fullNode:hover{
    background: url(images/full_hover_bg.png) no-repeat;
}
    </style>
</head>
<body>
    <!-- 最外层的元素 -->
    <div class='outerNode'>
        <!-- video元素 -->
        <video class='videoNode' src='data/imooc.mp4' poster="data/poster.jpg"></video>
        <!-- 操纵器的元素 -->
        <div class='controlsNode'>
            <!-- 操纵播发中止的按钮 -->
            <div class='playNode'></div>
            <!-- video的进度条 -->
            <div class='loadNode'>
                <div class='loadLeft'></div>
                <div class='loadRight'></div>
                <!-- 拖拽进度条的按钮 -->
                <div class='crlNode'></div>
                <!-- 真实的进度条 -->
                <div class='lineNode'>
                    <div class='lineRight'></div>
                </div>
            </div>
            <!-- 時间的元素 -->
            <div class='timeNode'>
                <span class='now'>00:00</span>
                <span> - </span>
                <span class='all'>4:30</span>
            </div>
            <!-- 响声的标示 -->
            <div class='volumeNode'></div>
            <!-- 响声的条 -->
            <div class='volumeLine'>
                <div class='v_left'></div>
                <div class='v_right'></div>
                <div class='v_DragNode'></div>
            </div>
            <!-- 全屏的按钮 -->
            <div class='fullNode'></div>
        </div>
    </div>
<script type="text/javascript">
    //播发中止的操纵
    //PlayNode 播发器按钮
    //VideoNode 播发器
    //PlayBln 操纵中止播发的布尔运算值
    //FullNode 全屏按钮
    //nowNode 当今時间
    //allNode 视頻的所有時间
    //LineNode 当今的进度条
    //CrlNode 进度条按钮
    //LoadNode 进度条外面的元素
    var PlayNode = document.getElementsByClassName('playNode')[0],
        VideoNode = document.getElementsByClassName('videoNode')[0],
        FullNode = document.querySelector('.fullNode'),
        nowNode = document.querySelector('.now'),
        allNode = document.querySelector('.all'),
        LineNode = document.querySelector('.lineNode'),
        CrlNode = document.querySelector('.crlNode'),
        LoadNode = document.querySelector('.loadNode'),
        VDragNode = document.querySelector('.v_DragNode'),
        PlayBln = true;
    //播发、中止的恶性事件
    PlayNode.onclick = function(){
        //传统式的根据布尔运算值去更改classname的方式
        PlayBln = !PlayBln;
        if(PlayBln == false){
            this.className = 'pauseNode';
            VideoNode.play();
        }
        else{
            this.className = 'playNode';
            VideoNode.pause();
        }
    };
    //全屏按钮的恶性事件
    FullNode.onclick = function(){
        if(VideoNode.webkitRequestFullscreen){
            VideoNode.webkitRequestFullscreen();
        }
        else if(VideoNode.mozRequestFullScreen){
            VideoNode.mozRequestFullScreen();
        }
        else{
            VideoNode.requestFullscreen();
        }
    };
    //处理最初時间的NAN的难题
    VideoNode.addEventListener('canplay',function(){
        var needTime = parseInt(VideoNode.duration);
        var  s = needTime%60;
        var  m = parseInt(needTime / 60);
        var timeNum = toDou(m)+':'+toDou(s);
        allNode.innerHTML = timeNum;
    },false);
    //处理 時间不够10 的难题
    function toDou(time){
        return time<10?'0'+time:time;
    }
    //当视頻播发的情况下 必须当今的時间动起来
    VideoNode.addEventListener('timeupdate',function(){
        //现阶段的 百分比进度
        LineNode.style.width = VideoNode.currentTime/VideoNode.duration*100+'%';
        CrlNode.style.left = LineNode.offsetWidth - 8.5 + 'px'
        var needTime = parseInt(VideoNode.currentTime);
        var  s = needTime%60;
        var  m = parseInt(needTime / 60);
        var timeNum = toDou(m)+':'+toDou(s);
        nowNode.innerHTML = timeNum;
    },false);
    //响声的拖拽按钮
    VDragNode.onmousedown = function(e){
        var ev = e || event;
        var l = ev.clientX - this.offsetLeft;
        document.onmousemove = function(e){
            var ev = e || event;
            var needX = ev.clientX - l;
            var maxX = VDragNode.parentNode.offsetWidth - 2.5;
            needX = needX < ⑵.5 ? - 2.5 : needX;
            needX = needX > maxX ? maxX : needX;
            //测算0 - 1
            var lastVolume = (VDragNode.offsetLeft + 2) / VDragNode.parentNode.offsetWidth ;
            VideoNode.volume = lastVolume < 0 ? 0 : lastVolume;
            VDragNode.style.left = needX + 'px';
        };
        document.onmouseup = function(e){
            document.onmousemove = document.onmouseup = null;
        }
        return false;
    }
    //拖拽进度条按钮
    CrlNode.onmousedown = function(e){
        var ev = e || event;
        var l = ev.clientX - this.offsetLeft;
        VideoNode.pause();
        document.onmousemove = function(e){
            var ev = e || event;
            var needX = ev.clientX - l;
            var maxX = LoadNode.offsetWidth - 8.5;
            needX = needX < ⑻.5 ? ⑻.5 : needX;
            needX = needX > maxX ? maxX : needX;
            CrlNode.style.left = needX + 'px';
            LineNode.style.width = (CrlNode.offsetLeft+9)/LoadNode.offsetWidth*100 + '%';
        };
        document.onmouseup = function(){
            document.onmousemove = document.onmouseup = null;
            VideoNode.currentTime = VideoNode.duration * (CrlNode.offsetLeft+9)/LoadNode.offsetWidth;
            if(PlayBln == false){
                //console.log(1);
                PlayNode.className = 'pauseNode';
                VideoNode.play();
            }
            else{
                //console.log(2);
                PlayNode.className = 'playNode';
                VideoNode.pause();
            }
        };
        return false;
    };
</script>
</body>
</html>

总结

以上所述是网编给大伙儿详细介绍的HTML5自定视頻播发器源代码,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑请给我留言,网编会立即回应大伙儿的。在此也十分谢谢大伙儿对脚本制作之家网站的适用!假如你感觉本文对你有协助,欢迎转载,烦请注明出处,感谢!