javascript实现简单的html5视频播放器_javascript技巧

效果:

代码很简单

js

define("html5_video_player", [ '../avalon-min'], function(avalon) {
  function formatTime(seconds) {
    var seconds = Math.round(seconds);
    var minutes = Math.floor(seconds / 60);
    seconds = Math.floor(seconds % 60);
    minutes = (minutes >= 10) ? minutes : "0" + minutes;
    seconds = (seconds >= 10) ? seconds : "0" + seconds;
    return minutes + ":" + seconds;
  }
  var playing=false,mute=false,vol=50,fs=false,active=false,inactivityTimeout=timer=null;
  avalon.bind($('control_btn'),'click',function(){
    if(!playing){
      $('html5_video').play();
      $('control_btn').className='html5_video_pause_btn inline-block';
      playing=true;
    }else{
      $('html5_video').pause();
      $('control_btn').className='html5_video_play_btn inline-block';
      playing=false;
    }
  });
  avalon.bind($('video_bar'),'click',function(e){
    var a=(e.clientX-$('video_bar').offsetLeft)/$('video_bar').offsetWidth;
    $('html5_video').currentTime =a.toFixed(2)*$('html5_video').duration;
  });
  avalon.bind($('vol_bar'),'click',function(e){
    var a=(e.clientX-$('vol_bar').offsetLeft-8)/$('vol_bar').offsetWidth;
    vol=$('html5_video').volume =a;
    $('vol_value').style.width=a*100+'%';
  });
  avalon.bind($('mute_icon'),'click',function(){
    if(!mute){
      $('html5_video').volume=0;
      $('mute_icon').className='html5_video_mute1';
      mute=true;
    }else{
      $('html5_video').volume=vol;
      $('mute_icon').className='html5_video_mute';
      mute=false;
    }
  });
  avalon.bind($('html5_video'),'loadedmetadata',function(){
    $('html5_video_duration').innerHTML=formatTime($('html5_video').duration);
    $('html5_video').volume=0;
  });
  avalon.bind($('html5_video'),'timeupdate',function(){
    $('html5_play_time').innerHTML=formatTime($('html5_video').currentTime);
    $('video_progress_bar').style.left=$('html5_video').currentTime/$('html5_video').duration*100+'%';
  });
  avalon.bind($('html5_video_fullscreen'),'click',function(e){
    if(!fs){
      toggle_fullscreen();
    }else{
      exit_fullscreen();
    }
  });
  document.onmozfullscreenchange = function() {
    if ($('html5_video').clientWidth +2!= document.documentElement.clientWidth) {
      exit_fullscreen();
    }
  };
  document.onwebkitfullscreenchange = function() {
    if ($('html5_video').clientWidth!= document.documentElement.clientWidth) {
      exit_fullscreen();
    }
  };
  function exit_fullscreen() {
    $('html5_video').className='';
    fs = false;
    active=false;
    $('video_control').className='';
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    }
  }
  function toggle_fullscreen() {
    $('html5_video').className='video_fs';
    fs = true;
    $('video_control').className='fullscreen';
    var elem=$('html5_video');
    if (elem.msRequestFullscreen) {
      elem.msRequestFullscreen();
    } else if (elem.mozRequestFullScreen) {
      elem.mozRequestFullScreen();
    } else if (elem.webkitRequestFullscreen) {
      elem.webkitRequestFullscreen();
    }
  }
  function updateBuffered() {
     var v = $('html5_video');
     var r = v.buffered;
     if (r) {
      for (var i=0; i<r.length; i++) {
       var start = r.start(i);
       var end = r.end(i);
      }
      $('video_buffer_bar').style.width=end/$('html5_video').duration*100+'%';
     }
    }
  setInterval(updateBuffered,500);
  function b(){
    if(active){
      $('video_control').style.display='none';
      active=false;
      console.log(active);
    }
  }
  avalon.bind($('html5_video'),'mousemove',function(e){
    if(fs){
      clearTimeout(inactivityTimeout);
      active=true;
      $('video_control').style.display='block';
      inactivityTimeout = setTimeout(b, 5000);
    }
  });
});

html

<link type="text/css"
  href="http://localhost/twitter/css/html5_video_player.css"
  rel="stylesheet" />
<div id='wrap_html5_video'>
  <div id='html5_video_area'>
    <video id="html5_video" width="360" height="240">
      <source type=" video/mp4" src="http://localhost/twitter/videos/2.mp4"></source>
      <source type=" video/webm"
        src="http://localhost/twitter/videos/2.webm"></source>
    </video>
  </div>
  <div id='video_control'>
    <div id='video_bar'>
      <div id='video_buffer_bar'></div>
      <div id='video_progress_bar'></div>
    </div>
    <div id='play_control'>
      <ul>
        <li class='inline-block'><a
          class='html5_video_play_btn inline-block' id='control_btn'></a></li>
        <li class='inline-block'><a id='mute_icon'
          class='html5_video_mute'></a>
          <div id='vol_bar' class='inline-block'>
            <p id='vol_value'></p>
          </div></li>
        <li class='inline-block' id='html5_video_time'><span
          id='html5_play_time'>00:00</span><span>/</span><span
          id='html5_video_duration'>33:44</span></li>
        <li class='inline-block'><a id='html5_video_fullscreen'
          class='inline-block'></a></li>
      </ul>
    </div>
    <div id='a'></div>
  </div>
  <div id='buffered_log'></div>
</div>
<script type="text/javascript">
  require('html5/html5_video_player');
</script>

css

@CHARSET "UTF-8";

#wrap_html5_video {
  padding: 10px;
  width: 360px;
}

#vol_bar,#video_bar,#vol_value {
  height: 9px;
  background-color: #999999;
}

#vol_bar {
  width: 100px;
  cursor: pointer;
}

#vol_value {
  background-color: #179df7;
  width: 50%;
}

#html5_video {
  display: block;
  border: 1px solid #c0deed;
}

#video_buffer_bar {
  background-color: #179DF7;
  width: 0;
}

#video_progress_bar,#video_buffer_bar {
  position: absolute;
  height: 100%;
}

#video_progress_bar {
  background-color: #0066FF;
  width: 2px;
  left: 0;
}

.html5_video_pause_btn,.html5_video_play_btn {
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.html5_video_play_btn {
  background: url("http://localhost/twitter/images/html5_video.jpg") 0 0
    no-repeat;
}

.html5_video_play_btn:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -41px
    0 no-repeat;
}

.html5_video_pause_btn {
  background: url("http://localhost/twitter/images/html5_video.jpg") 0
    -42px no-repeat;
}

.html5_video_pause_btn:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -41px
    -42px no-repeat;
}

#play_control a,#vol_bar {
  vertical-align: middle;
}

#html5_video_fullscreen {
  width: 25px;
  background: url("http://localhost/twitter/images/html5_video.jpg") 0
    -310px no-repeat;
  height: 18px;
}

#play_control #html5_video_time {
  font-size: 14px;
}

#play_control li,#play_control ul {
  font-size: 0;
}

#play_control li:last-child {
  position: absolute;
  right: 0;
}

.html5_video_mute1 {
  background: url("http://localhost/twitter/images/html5_video.jpg")
    no-repeat scroll -79px -170px rgba(0, 0, 0, 0);
}

.html5_video_mute {
  background: url("http://localhost/twitter/images/html5_video.jpg")
    no-repeat scroll 0 -170px rgba(0, 0, 0, 0);
}

#mute_icon {
  cursor: pointer;
  display: inline-block;
  height: 15px;
  width: 18px;
}

.html5_video_mute:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -19px
    -170px no-repeat;
}

#play_control li {
  height: 40px;
  vertical-align: top;
  margin: 0 5px;
}

#play_control li:after {
  display: inline-block;
  width: 0;
  height: 100%;
  vertical-align: middle;
  content: '';
}

#play_control,#video_bar,#vol_bar {
  position: relative;
}

body .fullscreen {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  overflow: hidden;
  z-index: 2147483647;
  background-color: #fff;
}

video::-webkit-media-controls {
  display: none !important;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索javascript
html5视频播放器
javascript音乐播放器、javascript视频播放器、javascript播放器、简单播放器、简单的播放器,以便于您获取更多的相关知识。

时间: 2024-08-01 00:36:38

javascript实现简单的html5视频播放器_javascript技巧的相关文章

javascript实现简单的html5视频播放器

  网页视频音频播放器大家并不陌生,在IE中我们可以运行ActiveX来嵌入微软的Media Player或者其他的本地播放器,当然可能大部分我们都是使用Flash来制作播放器.在HTML5发展迅速的今天,让我们尝试用HTML5来制作网页播放器吧,毕竟无论是PC还是移动设备,HTML5是未来的趋势 效果: 代码很简单 js ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3

JavaScript实现简单的tab选项卡切换_javascript技巧

本文实例讲解了JavaScript实现简单的tab选项卡切换的示例代码,分享给大家供大家参考,具体内容如下 效果图: 具体代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>table切换</title> <style type="text/css"> *{ paddi

javascript实现简单的贪吃蛇游戏_javascript技巧

javascript实现简单的贪吃蛇游戏,功能很简单,代码也很实用,就不多废话了,小伙伴们参考注释吧. <html> <head> <meta http-equiv='content-type' content='text/html;charset=utf-8'> <title>贪吃蛇</title> <script type="text/javascript"> var map; //地图 var snake;

JavaScript实现简单Tip提示框效果_javascript技巧

本文实例讲述了JavaScript实现简单Tip提示框效果.分享给大家供大家参考,具体如下: // JavaScript Document document.write("<div id='tip' style='position:absolute; width:300px; z-index:1; background-color: #ffffff; border: 1px solid gray; overflow: visible;visibility: hidden;font-size

javascript实现简单的省市区三级联动_javascript技巧

当我们注册一个网站,会看到省市区三级联动,下面简单介绍一下 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus"> <meta name="Author" content="&q

javascript实现简单的on事件绑定_javascript技巧

实现一个简单的on和off方法 介绍: Event对象:    funcList: {}, //保存delegate所绑定的方法      ieFuncList :{} //保存ie下的绑定方法 Event 对象中的 on, off 方法,主要调用 Event.addEvent, Event.delegateHandle这两个方法    Event.addEvent: 调用底层的addEventListener添加监听事件    Event.delegateHandle: 当发生事件之后,随着事

纯javascript实现简单下拉刷新功能_javascript技巧

代码很简单,实现的功能却很实用,直接奉上代码 CSS: 复制代码 代码如下: <meta charset="utf-8" /> <title>Pull to Refresh</title> <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-sca

原生javascript实现简单的datagrid数据表格_javascript技巧

简单的datagrid 1.排序 自定义排序方式 2.编辑 3.拖拽 4.分页 5.单选 多选(ctrl) 线性选(shift) 6.文字render  就是给文字着色  比如 大于0红色  小于0绿色 7.对列的显示隐藏 8.分组 只是一个示例  没有什么与后台的借口 其实可以写几个回调就行了  里面有loading条 可以在没返回结果前一直显示 复制代码 代码如下:  <!DOCTYPE html > <html xmlns="http://www.w3.org/1999/

javascript 通用简单的table选项卡实现_javascript技巧

第一步:引用table.js 复制代码 代码如下: <script type="text/javascript" src="table.js"> </script> 第二步:定义选中的样式,比如"active",应用选项卡的块的ID,比如"sidebar",默认被选中的序号,比如第一个"0": 第三步:调用函数: 复制代码 代码如下: <script type="te