摘要:前言前段時間基于寫了一個自定義的播放器組件踩了一些小坑這里做一下復(fù)盤分享出來避免日后重復(fù)踩坑設(shè)計階段這里就直接放幾張完成后的播放狀態(tài)圖吧界面布局基本就是適配一把梭也比較容易需要實(shí)現(xiàn)的幾個功能基本都標(biāo)注出來了除了還有一個視頻加載失敗的下面就這
前言
前段時間基于vue寫了一個自定義的video播放器組件,踩了一些小坑, 這里做一下復(fù)盤分享出來,避免日后重復(fù)踩坑...
設(shè)計階段這里就直接放幾張完成后的播放狀態(tài)圖吧,界面布局基本就是flex+vw適配一把梭,也比較容易.
需要實(shí)現(xiàn)的幾個功能基本都標(biāo)注出來了; 除了還有一個視頻加載失敗的...下面就這屆上代碼了;剛開始構(gòu)思的時候考慮了一下功能的實(shí)現(xiàn)方式: 一是用原生的DOM操作,獲取video元素后,用addEventListener來監(jiān)聽; 二是用vue的方式綁定事件監(jiān)聽; 最后圖方便采用了兩者結(jié)合的方式,但是總感覺有點(diǎn)亂, 打算后期再做一下代碼格式優(yōu)化.
video組件實(shí)現(xiàn)過程 組件模板部分主要是播放器的幾種播放狀態(tài)的邏輯理清楚就好了, 即: 播放中,緩存中,暫停,加載失敗這幾種情況,下面按功能分別說一下
播放器初始化![]()
![]()
![]()
視頻加載失敗
點(diǎn)擊重試
這里有個坑點(diǎn)我就是當(dāng)父元素隱藏即display:none時,getBoundingClientRect()是獲取不到元素的尺寸數(shù)值的,后來查了MDN文檔,按上面說的改了一下border也沒有用,最后嘗試設(shè)置元素visibility屬性為hidden后發(fā)現(xiàn)就可以獲取了.
最后查了一下瀏覽器的解析規(guī)則, 發(fā)現(xiàn)display等于none節(jié)點(diǎn)不會解析到render樹中,但是visibility等于hidden的元素是會顯示在這棵樹里頭的。可能是這個原因?qū)е履貌坏皆氐某叽鐢?shù)據(jù), 因?yàn)閴焊鶝]被瀏覽器解析...
getBoundingClientRect() : 返回元素的大小及其相對于視口的位置, 這個api在計算元素相對位置的時候挺好用的.
init() { // 初始化video,獲取video元素 this.$video = this.$el.getElementsByTagName("video")[0]; this.initPlayer(); }, // 初始化播放器容器, 獲取video-player元素 // getBoundingClientRect()以client可視區(qū)的左上角為基點(diǎn)進(jìn)行位置計算 initPlayer() { const $player = this.$el; const $progress = this.$el.getElementsByClassName("progress")[0]; // 播放器位置 this.player.$player = $player; this.progressBar.$progress = $progress; this.player.pos = $player.getBoundingClientRect(); this.progressBar.pos = $progress.getBoundingClientRect() this.video.progress.width = Math.round($progress.getBoundingClientRect().width); },播放 && 暫停點(diǎn)擊
我這里把事件監(jiān)聽都放在只有滿足正在播放視頻才開始事件監(jiān)聽; 感覺原生監(jiān)聽和vue方式的監(jiān)聽混合在一起寫有點(diǎn)別扭...emem...這里需要對this.$video.play()做一個異常處理,防止video剛開始加載的時候失敗,如果視頻鏈接出錯,play方法調(diào)用不了會拋錯,后面我也用了video的error事件去監(jiān)聽播放時的錯誤
// 點(diǎn)擊播放 & 暫停按鈕 clickPlayBtn() { if (this.state.isLoading) return; this.isFirstTouch = false; this.state.playing = !this.state.playing; this.state.isEnd = false; if (this.$video) { // 播放狀態(tài) if (this.state.playing) { try { this.$video.play(); this.isPauseTouch = false; // 監(jiān)聽緩存進(jìn)度 this.$video.addEventListener("progress", e => { this.getLoadTime(); }); // 監(jiān)聽播放進(jìn)度 this.$video.addEventListener( "timeupdate", throttle(this.getPlayTime, 100, 1) ); // 監(jiān)聽結(jié)束 this.$video.addEventListener("ended", e => { // 重置狀態(tài) this.state.playing = false; this.state.isEnd = true; this.state.controlBtnShow = true; this.video.displayTime = "00:00"; this.video.progress.current = 0; this.$video.currentTime = 0; }); } catch (e) { // 捕獲url異常出現(xiàn)的錯誤 } } // 停止?fàn)顟B(tài) else { this.isPauseTouch = true; this.$video.pause(); } } },視頻控制條顯示和隱藏
這里需要加兩個開關(guān); 首次觸屏和暫停觸屏; 做一下顯示處理即可
// 觸碰播放區(qū) touchEnterVideo() { if (this.isFirstTouch) return; if (this.hideTimer) { clearTimeout(this.hideTimer); this.hideTimer = null; } this.state.controlBtnShow = true; this.state.controlBarShow = true; }, // 離開播放區(qū) touchLeaveVideo() { if (this.isFirstTouch) return; if (this.hideTimer) { clearTimeout(this.hideTimer); } // 暫停觸摸, 不隱藏 if (this.isPauseTouch) { this.state.controlBtnShow = true; this.state.controlBarShow = true; } else { this.hideTimer = setTimeout(() => { this.state.controlBarShow = false; // 加載中只顯示loading if (this.state.isLoading) { this.state.controlBtnShow = true; } else { this.state.controlBtnShow = false; } this.hideTimer = null; }, 3000); } },視頻錯誤處理和等待處理
這里錯誤直接用error事件, 加載中用stalled事件來監(jiān)聽視頻阻塞狀態(tài),等待數(shù)據(jù)加載用的waiting事件; 顯示對應(yīng)的loading動畫即可
// loading動畫 @keyframes rotate { 0% { transform: rotate(0deg); } 50% { transform: rotate(180deg); } 100% { transform: rotate(360deg); } } .loader { width: 58px; height: 58px; background: rgba(15, 16, 17, 0.3); border-radius: 50%; position: relative; .ball-clip-rotate { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); > div { width: 15px; height: 15px; border-radius: 100%; margin: 2px; animation-fill-mode: both; border: 2px solid #fff; border-bottom-color: transparent; height: 26px; width: 26px; background: transparent; display: inline-block; animation: rotate 0.75s 0s linear infinite; } } }播放時間設(shè)置
基本就是video對象的currentTime和duration這兩個屬性; 這里注意下視頻如果沒有設(shè)置預(yù)加載屬性preload的話,在video元素初始化的時候是獲取不到duration的...那你只能在播放的時候去拿了.
// 獲取播放時間 getPlayTime() { const percent = this.$video.currentTime / this.$video.duration; this.video.progress.current = Math.round( this.video.progress.width * percent ); // 賦值時長 this.video.totalTime = timeParse(this.$video.duration); this.video.displayTime = timeParse(this.$video.currentTime); }, // 獲取緩存時間 getLoadTime() { // console.log("緩存了...",this.$video.buffered.end(0)); this.video.loaded = (this.$video.buffered.end(0) / this.$video.duration) * 100; },手動滑動進(jìn)度條控制
這里直接用touch事件即可; 注意touchend中使用e.changedTouches;因?yàn)楫?dāng)手指離開屏幕,touches和targetTouches中對應(yīng)的元素會同時移除,而changedTouches仍然會存在元素。
touches: 當(dāng)前屏幕上所有觸摸點(diǎn)的列表; targetTouches: 當(dāng)前對象上所有觸摸點(diǎn)的列表; changedTouches: 涉及當(dāng)前(引發(fā))事件的觸摸點(diǎn)的列表
// 手動調(diào)節(jié)播放進(jìn)度 moveStart(e) {}, moveIng(e) { // console.log("觸摸中..."); let currentX = e.targetTouches[0].pageX; let offsetX = currentX - this.progressBar.pos.left; // 邊界檢測 if (offsetX <= 0) { offsetX = 0 } if (offsetX >= this.video.progress.width) { offsetX = this.video.progress.width } this.video.progress.current = offsetX; let percent = this.video.progress.current / this.video.progress.width; this.$video.duration && this.setPlayTime(percent, this.$video.duration) }, moveEnd(e) { // console.log("觸摸結(jié)束..."); let currentX = e.changedTouches[0].pageX; let offsetX = currentX - this.progressBar.pos.left; this.video.progress.current = offsetX; // 這里的offsetX都是正數(shù) let percent = offsetX / this.video.progress.width; this.$video.duration && this.setPlayTime(percent, this.$video.duration) }, // 設(shè)置手動播放時間 setPlayTime(percent, totalTime) { this.$video.currentTime = Math.floor(percent * totalTime); },全屏功能
這個功能在手機(jī)上會有寫兼容性問題...有待完善
// 設(shè)置全屏 fullScreen() { console.log("點(diǎn)擊全屏..."); if (!this.state.fullScreen) { this.state.fullScreen = true; this.$video.webkitRequestFullScreen(); } else { this.state.fullScreen = false; document.webkitCancelFullScreen(); }坑點(diǎn)匯總
1.視頻預(yù)加載才能獲取時長 需要設(shè)置預(yù)加載 preload="auto" 2.Element.getBoundingClientRect()方法返回元素的大小及其相對于視口的位置 父元素設(shè)置display:none時獲取不到尺寸數(shù)據(jù)民謠改為visibility:hidden 3.play()方法異常捕獲 try{ xxxxx.play } catch(e) { yyyyyy } 4.安卓手機(jī)video兼容性處理, 視頻播放時層級置頂,會影響全局彈出層樣式 我這里做的處理是當(dāng)彈出層出現(xiàn)時把視頻給隱藏掉(寬高為0,或者直接去掉),用封面圖來替代 5.ios下全屏處理 設(shè)置相應(yīng)屬性即可, playsinline
代碼直通車: https://github.com/appleguard...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/105164.html
摘要:里面主要的是多段視頻播放暫停。本文包含全屏適配播放并在視頻上放置其他元素。蘋果手機(jī)嵌入視頻小窗播放。目前替換幾種解決方案的實(shí)測。安卓會自動全屏播放。如下圖解決方法監(jiān)聽屏幕全屏事件中手動設(shè)置的值為。 前兩天,美團(tuán)推出的楊洋H5火爆朋友圈。里面主要的是多段視頻播放、暫停。聽起來很簡單,但是由于騰訊白名單限制,在微信瀏覽器,qq瀏覽器,會自動將video標(biāo)簽中非騰訊域名的視頻 ,自動全屏,結(jié)...
閱讀 3448·2021-11-25 09:43
閱讀 2334·2021-09-06 15:02
閱讀 3572·2021-08-18 10:21
閱讀 3363·2019-08-30 15:55
閱讀 2380·2019-08-29 17:06
閱讀 3560·2019-08-29 16:59
閱讀 993·2019-08-29 13:47
閱讀 2798·2019-08-26 13:24