zoukankan      html  css  js  c++  java
  • H5自定义video功能与样式处理

    H5的video非常简单,方便,有时我们可能需要自己来设置样式来自定义的video,自定义的话我们需要对功能进行一些处理,这里常用的功能几乎是都用到了,第一次练习代码很累赘,之后会慢慢改进。

    常用的一些 video API

    "视频播放":video.play();

    "视频暂停播放":video.pause();

    "视频地址":video.currentSrc;

    "视频总时长":video.duration;

    "视频播放速率":video.playbackRate;

    "是否暂停":video.paused;

    "是否结束":video.ended;

    "是否静音":video.muted;  

    "当前播放时间": video.currentTime;

    "当前缓冲量":video.buffered.end(0);

    "当前音量":video.volume

    api使用方法

    video.onloadedmetadata=function(){
                    console.log("视频地址"+video.currentSrc);
                    console.log("视频总时长"+video.duration);
                    console.log("视频播放速率"+video.playbackRate);
                    console.log("是否暂停"+video.paused);
                    console.log("是否结束"+video.ended);
                    console.log("是否静音"+video.muted);                
                }
                //当前时间
                video.ontimeupdate=function(){
                    console.log("当前播放时间"+video.currentTime);
                    console.log("当前缓冲量"+video.buffered.end(0));
                }
                //当前音量
                video.onvolumechange=function(){
                    console.log("当前音量"+video.volume);
                }
                //播放
                btnPlay.onclick=function (){
                    video.play();
                }
                //停止
                btnStop.onclick=function (){
                    video.pause();
                }

    demo全部代码(代码里面的图片请替换成本地图片,不让无法看到此demo的完整效果,播放按钮在进度条左边)

    <!DOCTYPE html>
    <html>

        <head>
            <meta charset="UTF-8">

            <title>自定义播放器音量拖动</title>
            <style>
                .video-warp {
                     800px;
                    margin: auto;
                    text-align: center;
                }
                
                video {
                     100%;
                }
                
                .controls {
                     100%;
                    background: #ccc;
                    height: 20px;
                    line-height: 20px;
                    text-align: left;
                    box-sizing: border-box;
                    padding-left: 5px;
                    position: relative;
                }
                
                .controls i {
                    display: inline-block;
                     20px;
                    height: 20px;
                    background: url(img/play.png) no-repeat;
                    background-size: 100%;
                }
                
                .controls i:nth-of-type(2) {
                    display: none;
                    background: url(img/pause.png) no-repeat;
                    background-size: 100%;
                }
                
                .controls i:nth-of-type(3) {
                    background: url(img/sound.png) no-repeat;
                    background-size: 100%;
                }
                
                .controls i:nth-of-type(4) {
                    display: none;
                    background: url(img/mute.png) no-repeat;
                    background-size: 100%;
                }
                
                .progress {
                    display: inline-block;
                     62%;
                    height: 10px;
                    line-height: 10px;
                    background: #e5e9ef;
                    border-radius: 5px;
                    vertical-align: super;
                    overflow: hidden;
                }
                
                .video-timer {
                    display: inline-block;
                    vertical-align: top;
                }
                
                .timrBar {
                    display: inline-block;
                    height: 11px;
                    background: #8adced;
                }
                
                #playSpeed {
                    color: #fb0606;
                    cursor: pointer;
                    float: right;
                    margin-right: -32px;

                }
                /*yinliang*/
                
                .video-sound {
                    position: absolute;
                     100px;
                    height: 10px;
                    background: #e5e9ef;
                    bottom: 5px;
                    right:48px;
                    border-radius: 5px;
                    overflow: hidden;
                }
                
                .soundBar {
                    height: 100%;
                    background: #00a1d6;
                }
                /*全屏*/
                #screen{
                    float: right;
                    margin-right: 25px;
                    margin-top: 5px;
                     10px;
                    height: 10px;
                    background: url(img/full-screen.png);
                    background-size:contain ;
                }
            </style>
        </head>

        <body>
            <div class="video-warp" id="video-warp">
                <video id="video" poster="img/preview.jpg"                 
                    <source src="http://www.w3school.com.cn/i/movie.ogg" type="video/ogg"></source>               
                    当前浏览器不支持 video直接播放,点击这里下载视频:
                    <a href="myvideo.webm">下载视频</a>
                </video>
                <div class="controls" id="controls">
                    <i id="play"></i>
                    <i id="pause" class="hide"></i>
                    <!--进度条-->
                    <div class="progress">
                        <div class="timrBar"></div>
                    </div>
                    <!--时长-->
                    <div class="video-timer">
                        <span id="currentTime">00:00</span><em>/</em>
                        <span id="duration">00:00</span>
                    </div>
                    <i id="sound" class="sound"></i>
                    <i id="muteSound" class="sound"></i>
                    <div class="video-sound">
                        <div class="soundBar"></div>
                    </div>
                    <i id="screen"></i>
                    <span id="playSpeed">*3</span>
                </div>
            </div>
            
            <script>
                var v = {
                    video: document.getElementById("video"),//容器框
                    play: document.getElementById("play"),//播放按钮
                    pause: document.getElementById("pause"),//暂停按钮
                    duration: document.getElementById("duration"),//总时长
                    currentTime: document.getElementById("currentTime"),//当前播放时间
                    progress: document.getElementsByClassName("progress")[0],//进度条容器
                    timrBar: document.getElementsByClassName("timrBar")[0], //进度条

                    sound: document.getElementsByClassName("video-sound")[0], //音量容器    
                    soundBar: document.getElementsByClassName("soundBar")[0],//音量
                    playSpeed: document.getElementById("playSpeed"),//播放速率
                    warp: document.getElementById("video-warp"), //视频区域距离左边距离
                    soundPercent:0 ,//音量百分比
                    fullScreen:document.getElementById("screen")/*全屏按钮*/
                };

                v.video.onloadedmetadata = function() {                
                    //播放
                    v.play.onclick = function() {
                        if(v.video.paused || v.video.ended) {
                            v.video.play();
                            this.style.display = "none";
                            v.pause.style.display = "inline-block";
                        }
                    }
                    //暂停
                    v.pause.onclick = function() {
                        if(!v.video.paused || !v.video.ended) {
                            v.video.pause();
                            v.pause.style.display = "none";
                            v.play.style.display = "inline-block";
                        }
                    }
                    //获取时长
                    v.duration.innerHTML = timer(v.video.duration);
                    v.currentTime.innerHTML = timer(v.video.currentTime);
                    //进度条跟随
                    v.video.ontimeupdate = function() {
                        var currentTime = v.video.currentTime;
                        var duration = v.video.duration;
                        var percent = currentTime / duration * 100;
                        v.timrBar.style.width = percent + "%";
                        v.currentTime.innerHTML = timer(v.video.currentTime);
                    }
                    //进度条获取坐标调用 拖拽实现方法
                    var enableProgressDrag = function(e) {
                        var progressDrag = false;
                        v.progress.onmousedown = function(e) {
                            progressDrag = true;
                            updateprogress(e.pageX - v.warp.offsetLeft);
                        }
                        document.onmouseup = function(e) {
                            if(progressDrag) {
                                progressDrag = false;
                                updateprogress(e.pageX - v.warp.offsetLeft);
                            }

                        }
                        v.progress.onmousemove = function(e) {
                            if(progressDrag) {
                                updateprogress(e.pageX - v.warp.offsetLeft);
                            }
                        }
                    };

                    enableProgressDrag();
                    //播放速率
                    v.playSpeed.onclick = function() {
                        v.video.playbackRate = 3;
                    }

                    //音量获取坐标调用 拖拽实现方法
                    var enableSoundDrag = function(e) {
                        var soundDrag = false;
                        v.sound.onmousedown = function(e) {
                            soundDrag = true;
                            updatesound(e.pageX - v.warp.offsetLeft);
                        }
                        v.sound.onmouseup = function(e) {
                            if(soundDrag) {
                                soundDrag = false;
                                updatesound(e.pageX - v.warp.offsetLeft);
                            }

                        }
                        v.sound.onmousemove = function(e) {
                            if(soundDrag) {
                                updatesound(e.pageX - v.warp.offsetLeft);
                            }
                        }
                    };
                    enableSoundDrag();
                    updatesound(null, 35); //初始化音量
                    /*全屏*/
                    var isScreen=false;            
                    v.fullScreen.addEventListener("click",function(){
                        if(isScreen==false){
                            
                            requestFullscreen(v.video);
                            isScreen=true;
                        }else{
                            exitFullscreen(v.video);
                            isScreen=false;
                        }
                    })
                }
                //进度条可拖拽实现
                function updateprogress(x) {
                    var percent = 100 * (x - v.progress.offsetLeft) / v.progress.offsetWidth;
                    if(percent > 100) {
                        percent = 100;
                    }
                    if(percent < 0) {
                        percent = 0;
                    }
                    v.timrBar.style.width = percent + "%";
                    v.video.currentTime = v.video.duration * percent / 100;

                }
                //音量可拖拽实现
                function updatesound(x, n) {
                    sounding();
                    if(n) {
                        soundPercent = n;
                    } else {
                        soundPercent = 100 * (x - v.sound.offsetLeft) / v.sound.offsetWidth;
                    }
                    if(soundPercent > 100) {
                        persoundPercentcent = 100;
                    }
                    if(soundPercent < 0) {
                        soundPercent = 0;
                    }
                    v.soundBar.style.width = soundPercent + "%";
                    v.video.volume = soundPercent / 100;
                }
                //时间格式化            
                function timer(seconds) {
                    var minute = Math.floor(seconds / 60);
                    if(minute < 10) {
                        minute = "0" + minute;
                    }

                    var second = Math.floor(seconds % 60);
                    if(second < 10) {
                        second = "0" + second;
                    }
                    return minute + ":" + second;
                }
                //是否静音
                var sound = document.querySelector("#sound");
                var muteSound = document.querySelector("#muteSound");
                function sounding() {                
                    if(v.video.volume == 0 ) {
                        sound.style.display = "none";
                        muteSound.style.display = "inline-block";
                    } else {
                        sound.style.display = "inline-block";
                        muteSound.style.display = "none";
                    }
                }
                //切换静音
                sound.onclick=function(){
                    showHide(sound,muteSound);
                    v.video.volume =0;
                    v.soundBar.style.width=0;
                }
                //去除静音 音量回到之前选定区域
                muteSound.onclick=function(){
                    showHide(muteSound,sound);
                    v.soundBar.style.width = soundPercent + "%";
                    v.video.volume = soundPercent / 100;
                }
                //显示与隐藏
                function showHide(a,b){
                    a.style.display = "none";
                    b.style.display = "inline-block";
                }

                /*全屏*/
                // 全屏
                // ele:全屏的对象                        
                function requestFullscreen(ele) {
                    // 全屏兼容代码
                    if (ele.requestFullscreen) {
                        ele.requestFullscreen();
                    } else if (ele.webkitRequestFullscreen) {
                        ele.webkitRequestFullscreen();
                    } else if (ele.mozRequestFullScreen) {
                        ele.mozRequestFullScreen();
                    } else if (ele.msRequestFullscreen) {
                        ele.msRequestFullscreen();
                    }
                }
                
                // 取消全屏
                function exitFullscreen() {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.webkitExitFullscreen) {
                        document.webkitExitFullscreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    }
                }
            </script>
        </body>

    </html>

     效果:

  • 相关阅读:
    Lecture04_转换控制_GAMES101 课堂笔记——2020.2.21
    自动求梯度(pytorch版本)——2020.2.20
    深度学习之线性回归从零实现
    Lecture03_Transformation(变换)_GAMES101 课堂笔记——2020.2.18
    使用jupyter切换子环境,以及导致的`找不到指定模块`和`找不到指定的程序`问题
    多层感知机从0开始实现(Pytorch版本)——2020.2.16
    《动手学深度学习》(pytorch版本)中`d2lzh_pytorch`包问题
    Lecture02_向量与线性代数_GAMES101 课堂笔记——2020.2.14
    数据结构与算法(24)——优先队列和二叉堆
    剑指 Offer 06. 从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/zimengxiyu/p/10123054.html
Copyright © 2011-2022 走看看