h5视频播放器

一.首先先看一下目录
css框架用的是bootstrap,js库用的是jquery

目录.png

bootstrap
----|css
-------|bootstrap.css
----|fonts
-------|glyphicons-halflings-regular.eot
-------|glyphicons-halflings-regular.svg
-------|glyphicons-halflings-regular.ttf
-------|glyphicons-halflings-regular.woff
-------|glyphicons-halflings-regular.woff2
css
----|reset.css
----|willesPlay.css
images
----|control_01.png
----|playheader.jpg
js
----|jquery-1.11.3.min.js
----|willesPlay.js

二.先看一下index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<title>超炫酷的HTML5视频播放器DEMO演示</title>
<!--引用背景的css样式reset.css-->
<link rel="stylesheet" type="text/css" href="css/reset.css"/>
<!--引用bootstrap框架(css)-->
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<!--引用播放器的样式willesPlay.css-->
<link rel="stylesheet" type="text/css" href="css/willesPlay.css"/>
<!--引用jquery框架-->
<script src="js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
<!--引用视频的播放的js-->
<script src="js/willesPlay.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div id="willesPlay">
    <div class="playHeader">
        <div class="videoName">艾欧尼亚之血色月光</div>
    </div>
    <div class="playContent">
        <div class="turnoff">
            <ul>
                <li><a href="javascript:;" title="喜欢" class="glyphicon glyphicon-heart-empty"></a></li>
                <li><a href="javascript:;" title="关灯" class="btnLight on glyphicon glyphicon-sunglasses"></a></li>
                <li><a href="javascript:;" title="分享" class="glyphicon glyphicon-share"></a></li>
            </ul>
        </div>
        <video width="100%" height="100%" id="playVideo">
            <source src="0001.mp4" type="video/mp4"></source>
            当前浏览器不支持 video直接播放,点击这里下载视频: <a href="/">下载视频</a>
        </video>
        <div class="playTip glyphicon glyphicon-play"></div>
    </div>
    <div class="playControll">
        <div class="playPause playIcon"></div>
        <div class="timebar">
            <span class="currentTime">0:00:00</span>
            <div class="progress">
                <div class="progress-bar progress-bar-danger progress-bar-striped" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div>
                </div>
            <span class="duration">0:00:00</span>
        </div>
        <div class="otherControl">
            <span class="volume glyphicon glyphicon-volume-down"></span>
            <span class="fullScreen glyphicon glyphicon-fullscreen"></span>
            <div class="volumeBar">
                    <div class="volumewrap">
                        <div class="progress">
                        <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 8px;height: 40%;"></div>
                    </div>
                        </div>
                </div>
        </div>
    </div>
</div>
            
        </div>
    </div>
</div>

<div style="text-align:center;clear:both;">
    <script src="/gg_bd_ad_720x90.js" type="text/javascript"></script>
    <script src="/follow.js" type="text/javascript"></script>
</div>
</body>
</html>

三.再看一下播放器的js

$(function() {
    var playVideo = $('video');
    var playPause = $('.playPause'); //播放和暂停
    var currentTime = $('.timebar .currentTime'); //当前时间
    var duration = $('.timebar .duration'); //总时间
    var progress = $('.timebar .progress-bar'); //进度条
    var volumebar = $('.volumeBar .volumewrap').find('.progress-bar');
    playVideo[0].volume = 0.4; //初始化音量
    playPause.on('click', function() {
        playControl();
    });
    $('.playContent').on('click', function() {
        playControl();
    }).hover(function() {
        $('.turnoff').stop().animate({
            'right': 0
        }, 500);
    }, function() {
        $('.turnoff').stop().animate({
            'right': -40
        }, 500);
    });
    $(document).click(function() {
        $('.volumeBar').hide();
    });
    playVideo.on('loadedmetadata', function() {
        duration.text(formatSeconds(playVideo[0].duration));
    });

    playVideo.on('timeupdate', function() {
        currentTime.text(formatSeconds(playVideo[0].currentTime));
        progress.css('width', 100 * playVideo[0].currentTime / playVideo[0].duration + '%');
    });
    playVideo.on('ended', function() {
        $('.playTip').removeClass('glyphicon-pause').addClass('glyphicon-play').fadeIn();
        playPause.toggleClass('playIcon');
    });
    
    $(window).keyup(function(event){
        event = event || window.event;
            if(event.keyCode == 32)playControl();
            if(event.keyCode == 27){
            $('.fullScreen').removeClass('cancleScreen');
            $('#willesPlay .playControll').css({
                'bottom': -48
            }).removeClass('fullControll');
            };
        event.preventDefault();
    });
    
    
    //全屏
    $('.fullScreen').on('click', function() {
        if ($(this).hasClass('cancleScreen')) {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.mozExitFullScreen) {
                document.mozExitFullScreen();
            } else if (document.webkitExitFullscreen) {
                document.webkitExitFullscreen();
            }
            $(this).removeClass('cancleScreen');
            $('#willesPlay .playControll').css({
                'bottom': -48
            }).removeClass('fullControll');
        } else {
            if (playVideo[0].requestFullscreen) {
                playVideo[0].requestFullscreen();
            } else if (playVideo[0].mozRequestFullScreen) {
                playVideo[0].mozRequestFullScreen();
            } else if (playVideo[0].webkitRequestFullscreen) {
                playVideo[0].webkitRequestFullscreen();
            } else if (playVideo[0].msRequestFullscreen) {
                playVideo[0].msRequestFullscreen();
            }
            $(this).addClass('cancleScreen');
            $('#willesPlay .playControll').css({
                'left': 0,
                'bottom': 0
            }).addClass('fullControll');
        }
        return false;
    });
    //音量
    $('.volume').on('click', function(e) {
        e = e || window.event;
        $('.volumeBar').toggle();
        e.stopPropagation();
    });
    $('.volumeBar').on('click mousewheel DOMMouseScroll', function(e) {
        e = e || window.event;
        volumeControl(e);
        e.stopPropagation();
        return false;
    });
    $('.timebar .progress').mousedown(function(e) {
        e = e || window.event;
        updatebar(e.pageX);
    });
    //$('.playContent').on('mousewheel DOMMouseScroll',function(e){
    //  volumeControl(e);
    //});
    var updatebar = function(x) {
        var maxduration = playVideo[0].duration; //Video 
        var positions = x - progress.offset().left; //Click pos
        var percentage = 100 * positions / $('.timebar .progress').width();
        //Check within range
        if (percentage > 100) {
            percentage = 100;
        }
        if (percentage < 0) {
            percentage = 0;
        }

        //Update progress bar and video currenttime
        progress.css('width', percentage + '%');
        playVideo[0].currentTime = maxduration * percentage / 100;
    };
    //音量控制
    function volumeControl(e) {
        e = e || window.event;
        var eventype = e.type;
        var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));
        var positions = 0;
        var percentage = 0;
        if (eventype == "click") {
            positions = volumebar.offset().top - e.pageY;
            percentage = 100 * (positions + volumebar.height()) / $('.volumeBar .volumewrap').height();
        } else if (eventype == "mousewheel" || eventype == "DOMMouseScroll") {
            percentage = 100 * (volumebar.height() + delta) / $('.volumeBar .volumewrap').height();
        }
        if (percentage < 0) {
            percentage = 0;
            $('.otherControl .volume').attr('class', 'volume glyphicon glyphicon-volume-off');
        }
        if (percentage > 50) {
            $('.otherControl .volume').attr('class', 'volume glyphicon glyphicon-volume-up');
        }
        if (percentage > 0 && percentage <= 50) {
            $('.otherControl .volume').attr('class', 'volume glyphicon glyphicon-volume-down');
        }
        if (percentage >= 100) {
            percentage = 100;
        }
        $('.volumewrap .progress-bar').css('height', percentage + '%');
        playVideo[0].volume = percentage / 100;
        e.stopPropagation();
        e.preventDefault();
    }

    function playControl() {
            playPause.toggleClass('playIcon');
            if (playVideo[0].paused) {
                playVideo[0].play();
                $('.playTip').removeClass('glyphicon-play').addClass('glyphicon-pause').fadeOut();
            } else {
                playVideo[0].pause();
                $('.playTip').removeClass('glyphicon-pause').addClass('glyphicon-play').fadeIn();
            }
        }
        //关灯
    $('.btnLight').click(function(e) {
        e = e || window.event;
        if ($(this).hasClass('on')) {
            $(this).removeClass('on');
            $('body').append('<div class="overlay"></div>');
            $('.overlay').css({
                'position': 'absolute',
                'width': 100 + '%',
                'height': $(document).height(),
                'background': '#000',
                'opacity': 1,
                'top': 0,
                'left': 0,
                'z-index': 999
            });
            $('.playContent').css({
                'z-index': 1000
            });
            $('.playControll').css({
                'bottom': -48,
                'z-index': 1000
            });

            $('.playContent').hover(function() {
                $('.playControll').stop().animate({
                    'height': 48,
                },500);
            }, function() {
                setTimeout(function() {
                    $('.playControll').stop().animate({
                        'height': 0,
                    }, 500);
                }, 2000)
            });
        } else {
            $(this).addClass('on');
            $('.overlay').remove();
            $('.playControll').css({
                'bottom': 0,
            });
        }
        e.stopPropagation();
        e.preventDefault();
    });
});

//秒转时间
function formatSeconds(value) {
    value = parseInt(value);
    var time;
    if (value > -1) {
        hour = Math.floor(value / 3600);
        min = Math.floor(value / 60) % 60;
        sec = value % 60;
        day = parseInt(hour / 24);
        if (day > 0) {
            hour = hour - 24 * day;
            time = day + "day " + hour + ":";
        } else time = hour + ":";
        if (min < 10) {
            time += "0";
        }
        time += min + ":";
        if (sec < 10) {
            time += "0";
        }
        time += sec;
    }
    return time;
}

最终效果就是:


阿卡丽和易大师对决1.png
阿卡丽和易大师对决2.png

源码地址:https://github.com/Feiyu123/H5Video.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容