H5打造屬於自己的影片播放器(JS篇1)

newsning發表於2016-08-16

回顧

1)H5打造屬於自己的影片播放器(HTML篇)
2)H5打造屬於自己的影片播放器(邏輯篇)

前言

在HTML篇當中我們寫好了樣式,今天我們為video新增一個樣式

.bad-video video {
    width: 100%;
    height: 100%;
    display: block;
    object-fit:fill;
}

高,寬,內容,都充父元素

乳此一來,我們的HMTL元素內容為

        <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>

吹著口哨寫JS

幸好我們在寫JS之前,先整理好了邏輯思緒,並寫好了筆記H5打造屬於自己的影片播放器(邏輯篇)

以下文章將要用到 號稱 最接近原生APP體驗的高效能前端框架MUI

首先,讓我們來新建一個js檔案,比如,bvd.js 並註冊一下全域性的一個入口

(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))

讓我們在video.html引入bvd.js 並且來測試一下吧!

<scrpit src="js/mui.min.js"></script>
<script src="js/bvd.js"></script>
        <script>
            var v = mui.bvd();
            v.test();
        </script>

clipboard.png

看來是ok的,與此同時,為了html頁的簡潔美觀,我們將播放按鈕控制條用JS動態生成

(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))

這裡有個小細節,如果img的src設定為本地資源的話,那麼以後使用會出現很多問題,比如,頁面層級發生變化時,你要去修改bvd.js,以免夜長夢多,我們將圖片轉為base64

再新增控制條

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+FfOBpwvVCcaIwWvfQSCVwA4Pp29AKLeg2H8OgA9BIIXAIh0fPjndVmMJ6IPiDglgFX3jTD+N9KK94AAbSJqI+K3pQgRonEYPDr0wUaoFQAmQKO0eokAg0IITTQDxDERTBSpybw1n7gGc8wcW/NWR6PuIEIHiLqAuF/EfAlgoJV+6zpHl7nUBoDjH349IKXfFODK3RLBWAfBuCoXLHFFu4hggkzPXRhgXEjU6uzyx69vXMbJ27dyAES7PlRvEKGXd9JGpAOokQ7Cmzp3j5l/vJ7gAED3XFQGEYx0oM+qXk+lADA7J9Dz61y+PNFnQhxqtTfyQXduAq+xZZT+q4dEfUD8hz3AaRKq1mFV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m9kCEYx18NW4a09c8Iemq/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);
        
        
    }

再把 video.html中的 播放按鈕 和 控制條 註釋,看看我們寫的程式碼效果如何

clipboard.png
大家可以看到,紅色是我們註釋的,綠色的是我們js生成的,奈斯

開始播放

真正開始我們js篇的第一步啦

初始化事件,先寫個播放按鈕播放影片事件測試一下

            //元素初始化
            that.initEm();
            //事件初始化
            that.initEvent();

----------

    pro.initEvent = function(){
        var that = this;
        //給播放按鈕圖片新增事件
        this.vimg.addEventListener('tap',function(){
            that.video.play();
        })
    }

tap事件,是MUI封裝好的一個適合移動端點選的事件哦,移動端click事件延遲300ms,建議大家多用tap,關於tap事件是如何模擬的,請看HTML5觸控事件演化tap事件介紹

喲西,影片開始播放拉!!!

clipboard.png

回顧一下H5打造屬於自己的影片播放器(邏輯篇)
溫習一下移動端HTML5<video>影片播放最佳化實踐

初始化

1)封面 在這裡我沒啥好圖,就不設定了,大家可以試試在video標籤加入屬性 poster="圖片地址"
2)獲取影片長度,ok,我們給影片新增一個"loadedmetadata"獲取到後設資料事件,什麼事後設資料?

舉個例子:一部電影30個G,名字叫《我將帶頭衝鋒》,影片長300小時

大小,名稱,長度,等這些應該就算是後設資料了。

我們給影片新增一個獲取到後設資料事件

    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;
        });
    }

clipboard.png

果然出現了影片的長度,可是這是按秒計算的啊。。。

這時我們可以寫一個時間轉換的函式

    function stom(t) {
        var m = Math.floor(t / 60);
        m < 10 && (m = '0' + m);
        return m + ":" + (t % 60 / 100).toFixed(2).slice(-2);
    }

轉換一下

clipboard.png

可以,這很清真,今天我們暫時講到這,消化訊息,也讓樓主整頓一下思緒

本章節JS1原始碼放在這裡

H5打造屬於自己的影片播放器(JS篇2)

相關文章