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

NaomiYancey 8年前
   <h2>回顾</h2>    <p>1) <a href="/misc/goto?guid=4959676822958329097" rel="nofollow,noindex">H5打造属于自己的视频播放器(HTML篇)</a></p>    <p>2) <a href="/misc/goto?guid=4959676823075637827" rel="nofollow,noindex">H5打造属于自己的视频播放器(逻辑篇)</a></p>    <h2>前言</h2>    <p>在HTML篇当中我们写好了样式,今天我们为video新增一个样式</p>    <pre>  <code class="language-css">.bad-video video {      width: 100%;      height: 100%;      display: block;      object-fit:fill;  }</code></pre>    <p>高,宽,内容,都充父元素</p>    <p>乳此一来,我们的HMTL元素内容为</p>    <pre>  <code class="language-html"><div class="bad-video">              <video webkit-playsinline>                  <source src='xx.mp4' type="video/mp4"></source>                  <p>设备不支持</p>              </video>                            <img src="img/play.png" class="vplay" />              <div class="controls">                  <div>                      <div class="progressBar">                          <div class="timeBar"></div>                      </div>                  </div>                  <div><span class="current">00:00</span>/<span class="duration">00:00</span></div>                  <div><span class="fill">全屏</span></div>              </div>          </div></code></pre>    <h2>吹着口哨写JS</h2>    <p>幸好我们在写JS之前,先整理好了逻辑思绪,并写好了笔记 <a href="/misc/goto?guid=4959676823075637827" rel="nofollow,noindex">H5打造属于自己的视频播放器(逻辑篇)</a></p>    <p>以下文章将要用到 号称 <strong>最接近原生APP体验的高性能前端框架</strong> 的 <a href="/misc/goto?guid=4959667075254791381" rel="nofollow,noindex"> <strong>MUI</strong> </a></p>    <p>首先,让我们来新建一个js文件,比如,bvd.js 并注册一下全局的一个入口</p>    <pre>  <code class="language-javascript">(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))</code></pre>    <p>让我们在video.html引入bvd.js 并且来测试一下吧!</p>    <pre>  <code class="language-html"><scrpit src="js/mui.min.js"></script>  <script src="js/bvd.js"></script>          <script>              var v = mui.bvd();              v.test();          </script></code></pre>    <p><img src="https://simg.open-open.com/show/e2c396e313cd47c0187da23c63f7f655.png"></p>    <p>看来是ok的,与此同时,为了html页的简洁美观,我们将 <strong>播放按钮</strong> 和 <strong>控制条</strong> 用JS动态生成</p>    <pre>  <code class="language-javascript">(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))</code></pre>    <p>这里有个小细节,如果img的src设置为本地资源的话,那么以后使用会出现很多问题,比如,页面层级发生变化时,你要去修改bvd.js,以免夜长梦多,我们将图片转为base64</p>    <p>再添加控制条</p>    <pre>  <code class="language-javascript">pro.initEm = function(){          //先添加播放按钮          this.vimg = document.createElement("img");          this.vimg.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAPXklEQVR4Xu1dTXYbNxKuQjPycjzLeW8mok8Q+gShT2DlBKakLCOZOoHpE5iWshxJ9AkincD0CUKfIGSS92YZe5mEjZqH7qZMUuyuQqN/0CK1swmggaoP9YdCAWH3t9UUwK1e/W7xsAPAloNgKwDQ6//yGMPWN8u8VqjbBNA2/4cAU01quvw7BfOPo+GTTw8dHw8KAL3+/9qo//4GgTpA0EagNiB2nZhINCbAKSBMCXBC6quPo+G/VsDiNH7NnRsNAMPwQP/1LRF0AamLgNGOLvuPgKZAOEaEcaj2PjQZEI0DQK//e0eF4QtAOqiK4RygEkDc6CB4Nxr+e8K19+n3RgAg2enPAagHgB2fCHh/LjQBwFGo9m6bIBm8BsD3P/zWJQxfAuKB30xPmR3RDVLw9r8//mfs6/y9BEDC+FfOBpwvVCca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Q1+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 = '<div><div class="progressBar"><div class="timeBar"></div></div></div><div><span class="current">00:00</span>/<span class="duration">00:00</span></div><div><span class="fill">全屏</span></div>';          this.vRoom.appendChild(this.vC);                          }</code></pre>    <p>再把 video.html中的 播放按钮 和 控制条 注释,看看我们写的代码效果如何</p>    <p><img src="https://simg.open-open.com/show/96c65ecfa5d3d944f708b9852f2488cb.png"> 大家可以看到,红色是我们注释的,绿色的是我们js生成的,奈斯</p>    <h2>开始播放</h2>    <p>真正开始我们js篇的第一步啦</p>    <p>初始化事件,先写个播放按钮播放视频事件测试一下</p>    <pre>  <code class="language-javascript">//元素初始化              that.initEm();              //事件初始化              that.initEvent();    ----------        pro.initEvent = function(){          var that = this;          //给播放按钮图片添加事件          this.vimg.addEventListener('tap',function(){              that.video.play();          })      }</code></pre>    <p>tap事件,是MUI封装好的一个适合移动端点击的事件哦,移动端click事件延迟300ms,建议大家多用tap,关于tap事件是如何模拟的,请看 <a href="/misc/goto?guid=4959676823232347772" rel="nofollow,noindex">HTML5触摸事件演化tap事件介绍</a></p>    <p>哟西,视频开始播放拉!!!</p>    <p><img src="https://simg.open-open.com/show/179af8d5a8cbe167685197265bf57661.png"></p>    <p>回顾一下 <a href="/misc/goto?guid=4959676823075637827" rel="nofollow,noindex">H5打造属于自己的视频播放器(逻辑篇)</a></p>    <p>温习一下 <a href="/misc/goto?guid=4959676823350459655" rel="nofollow,noindex">移动端HTML5<video>视频播放优化实践</a></p>    <h2>初始化</h2>    <p>1)封面 在这里我没啥好图,就不设置了,大家可以试试在video标签加入属性 preload="图片地址"</p>    <p>2)获取视频长度,ok,我们给视频添加一个" <strong>loadedmetadata</strong> "获取到元数据事件,什么事元数据?</p>    <p>举个栗子:一部电影30个G,名字叫《我将带头冲锋》,视频长300小时</p>    <p>大小,名称,长度,等这些应该就算是元数据了。</p>    <p>我们给视频添加一个获取到元数据事件</p>    <pre>  <code class="language-javascript">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;          });      }</code></pre>    <p><img src="https://simg.open-open.com/show/29b764981fca2921bbd04e038447ca94.png"></p>    <p>果然出现了视频的长度,可是这是按秒计算的啊。。。</p>    <p>这时我们可以写一个时间转换的函数</p>    <pre>  <code class="language-javascript">function stom(t) {          var m = Math.floor(t / 60);          m < 10 && (m = '0' + m);          return m + ":" + (t % 60 / 100).toFixed(2).slice(-2);      }</code></pre>    <p>转换一下</p>    <p><img src="https://simg.open-open.com/show/4e857f58ac0238747db80da9a15ac084.png"></p>    <p>可以,这很清真,今天我们暂时讲到这,消化消息,也让楼主整顿一下思绪</p>    <p>本章节JS1源码放在 <a href="/misc/goto?guid=4959676823466328105" rel="nofollow,noindex">这里</a></p>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000006569543</p>    <p> </p>    <p><span style="background:rgb(189, 8, 28) url("data:image/svg+xml; border-radius:2px; border:medium none; color:rgb(255, 255, 255); cursor:pointer; display:none; font:bold 11px/20px "Helvetica Neue",Helvetica,sans-serif; left:30px; opacity:0.85; padding:0px 4px 0px 0px; position:absolute; text-align:center; text-indent:20px; top:4295px; width:auto; z-index:8675309">Save</span></p>    <p> </p>