最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實(shí)現(xiàn)自定義H5視頻播放器的方法步驟

 更新時(shí)間:2019年07月01日 09:59:06   作者:appleguardu  
這篇文章主要介紹了vue實(shí)現(xiàn)自定義H5視頻播放器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

前段時(shí)間基于vue寫了一個(gè)自定義的video播放器組件,踩了一些小坑, 這里做一下復(fù)盤分享出來,避免日后重復(fù)踩坑...

設(shè)計(jì)階段

這里就直接放幾張完成后的播放狀態(tài)圖吧,界面布局基本就是flex+vw適配一把梭,也比較容易.

需要實(shí)現(xiàn)的幾個(gè)功能基本都標(biāo)注出來了; 除了還有一個(gè)視頻加載失敗的...下面就這屆上代碼了;剛開始構(gòu)思的時(shí)候考慮了一下功能的實(shí)現(xiàn)方式: 一是用原生的DOM操作,獲取video元素后,用addEventListener來監(jiān)聽; 二是用vue的方式綁定事件監(jiān)聽; 最后圖方便采用了兩者結(jié)合的方式,但是總感覺有點(diǎn)亂, 打算后期再做一下代碼格式優(yōu)化.

video組件實(shí)現(xiàn)過程

組件模板部分

主要是播放器的幾種播放狀態(tài)的邏輯理清楚就好了, 即: 播放中,緩存中,暫停,加載失敗這幾種情況,下面按功能分別說一下

<template>
 <div class="video-player">
  <!-- 播放器界面; 兼容ios controls-->
  <video
   ref="video"
   v-if="showVideo"
   webkit-playsinline="true"
   playsinline="true"
   x-webkit-airplay="true"
   x5-video-player-type="h5"
   x5-video-player-fullscreen="true"
   x5-video-orientation="portraint"
   style="object-fit:fill"
   preload="auto"
   muted="true"
   poster="https://photo.mac69.com/180205/18020526/a9yPQozt0g.jpg"
   :src="src"
   @waiting="handleWaiting"
   @canplaythrough="state.isLoading = false"
   @playing="state.isLoading = false, state.controlBtnShow = false, state.playing=true"
   @stalled="state.isLoading = true"
   @error="handleError"
  >您的瀏覽器不支持HTML5</video>
  <!-- 兼容Android端層級(jí)問題, 彈出層被覆蓋 -->
  <img
   v-show="!showVideo || state.isEnd"
   class="poster"
   src="https://photo.mac69.com/180205/18020526/a9yPQozt0g.jpg"
   alt
  >
  <!-- 控制窗口 -->
  <div
   class="control"
   v-show="!state.isError"
   ref="control"
   @touchstart="touchEnterVideo"
   @touchend="touchLeaveVideo"
  >
   <!-- 播放 || 暫停 || 加載中-->
   <div class="play" @touchstart.stop="clickPlayBtn" v-show="state.controlBtnShow">
    <img
     v-show="!state.playing && !state.isLoading"
     src="../../assets/video/content_btn_play.svg"
    >
    <img
     v-show="state.playing && !state.isLoading"
     src="../../assets/video/content_btn_pause.svg"
    >
    <div class="loader" v-show="state.isLoading">
     <div class="loader-inner ball-clip-rotate">
      <div></div>
     </div>
    </div>
   </div>
   <!-- 控制條 -->
   <div class="control-bar" :style="{ visibility: state.controlBarShow ? 'visible' : 'hidden'}">
    <span class="time">{{video.displayTime}}</span>
    <span class="progress" ref="progress">
     <img
      class="progress-btn ignore"
      :style="{transform: `translate3d(${video.progress.current}px, 0, 0)`}"
      src="../../assets/video/content_ic_tutu.svg"
     >
     <span class="progress-loaded" :style="{ width: `${video.loaded}%`}"></span>
     <!-- 設(shè)置手動(dòng)移動(dòng)的進(jìn)度條 -->
     <span
      class="progress-move"
      @touchmove.stop.prevent="moveIng($event)"
      @touchstart.stop="moveStart($event)"
      @touchend.stop="moveEnd($event)"
     ></span>
    </span>

    <span class="total-time">{{video.totalTime}}</span>
    <span class="full-screen" @click="fullScreen">
     <img src="../../assets/video/content_ic_increase.svg" alt>
    </span>
   </div>
  </div>
  <!-- 錯(cuò)誤彈窗 -->
  <div class="error" v-show="state.isError">
   <p class="lose">視頻加載失敗</p>
   <p class="retry" @click="retry">點(diǎn)擊重試</p>
  </div>
 </div>
</template>

播放器初始化

這里有個(gè)坑點(diǎn)我就是當(dāng)父元素隱藏即display:none時(shí),getBoundingClientRect()是獲取不到元素的尺寸數(shù)值的,后來查了MDN文檔,按上面說的改了一下border也沒有用,最后嘗試設(shè)置元素visibility屬性為hidden后發(fā)現(xiàn)就可以獲取了.
getBoundingClientRect() : 返回元素的大小及其相對(duì)于視口的位置, 這個(gè)api在計(jì)算元素相對(duì)位置的時(shí)候挺好用的.

  init() {
   // 初始化video,獲取video元素
   this.$video = this.$el.getElementsByTagName("video")[0];
   this.initPlayer();
  },
  // 初始化播放器容器, 獲取video-player元素
  // getBoundingClientRect()以client可視區(qū)的左上角為基點(diǎn)進(jìn)行位置計(jì)算
  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...這里需要對(duì)this.$video.play()做一個(gè)異常處理,防止video剛開始加載的時(shí)候失敗,如果視頻鏈接出錯(cuò),play方法調(diào)用不了會(huì)拋錯(cuò),后面我也用了video的error事件去監(jiān)聽播放時(shí)的錯(cuò)誤

// 點(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)的錯(cuò)誤
     }
    }
    // 停止?fàn)顟B(tài)
    else {
     this.isPauseTouch = true;
     this.$video.pause();
    }
   }
  },

視頻控制條顯示和隱藏

這里需要加兩個(gè)開關(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);
   }
  },

視頻錯(cuò)誤處理和等待處理

這里錯(cuò)誤直接用error事件, 加載中用stalled事件來監(jiān)聽視頻阻塞狀態(tài),等待數(shù)據(jù)加載用的waiting事件; 顯示對(duì)應(yīng)的loading動(dòng)畫即可

// loading動(dòng)畫
@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í)間設(shè)置

基本就是video對(duì)象的currentTime和duration這兩個(gè)屬性; 這里注意下視頻如果沒有設(shè)置預(yù)加載屬性preload的話,在video元素初始化的時(shí)候是獲取不到duration的...那你只能在播放的時(shí)候去拿了.

// 獲取播放時(shí)間
  getPlayTime() {
   const percent = this.$video.currentTime / this.$video.duration;
   this.video.progress.current = Math.round(
    this.video.progress.width * percent
   );
   // 賦值時(shí)長(zhǎng)
   this.video.totalTime = timeParse(this.$video.duration);
   this.video.displayTime = timeParse(this.$video.currentTime);
  },
  // 獲取緩存時(shí)間
  getLoadTime() {
   // console.log('緩存了...',this.$video.buffered.end(0));
   this.video.loaded =
    (this.$video.buffered.end(0) / this.$video.duration) * 100;
  },

手動(dòng)滑動(dòng)進(jìn)度條控制

這里直接用touch事件即可; 注意touchend中使用e.changedTouches;因?yàn)楫?dāng)手指離開屏幕,touches和targetTouches中對(duì)應(yīng)的元素會(huì)同時(shí)移除,而changedTouches仍然會(huì)存在元素。

  • touches: 當(dāng)前屏幕上所有觸摸點(diǎn)的列表;
  • targetTouches: 當(dāng)前對(duì)象上所有觸摸點(diǎn)的列表;
  • changedTouches: 涉及當(dāng)前(引發(fā))事件的觸摸點(diǎn)的列表
// 手動(dòng)調(diào)節(jié)播放進(jìn)度
  moveStart(e) {},
  moveIng(e) {
   // console.log("觸摸中...");
   let currentX = e.targetTouches[0].pageX;
   let offsetX = currentX - this.progressBar.pos.left;
   // 邊界檢測(cè)
   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è)置手動(dòng)播放時(shí)間
  setPlayTime(percent, totalTime) {
   this.$video.currentTime = Math.floor(percent * totalTime);
  },

全屏功能

這個(gè)功能在手機(jī)上會(huì)有寫兼容性問題...有待完善

// 設(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í)長(zhǎng)
需要設(shè)置預(yù)加載 preload="auto"
2.Element.getBoundingClientRect()方法返回元素的大小及其相對(duì)于視口的位置
父元素設(shè)置display:none時(shí)獲取不到尺寸數(shù)據(jù)民謠改為visibility:hidden
3.play()方法異常捕獲
try{ xxxxx.play } catch(e) { yyyyyy }
4.安卓手機(jī)video兼容性處理, 視頻播放時(shí)層級(jí)置頂,會(huì)影響全局彈出層樣式
我這里做的處理是當(dāng)彈出層出現(xiàn)時(shí)把視頻給隱藏掉(寬高為0,或者直接去掉),用封面圖來替代
5.ios下全屏處理
設(shè)置相應(yīng)屬性即可, playsinline

代碼直通車: https://github.com/appleguardu/vue-h5-video

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue.js快速入門教程

    Vue.js快速入門教程

    Vue.js是一個(gè)專注于視圖模型(ViewModal)的框架。接下來給大家?guī)砹藇ue.js快速入門教程,非常不錯(cuò),具有參考借鑒價(jià)值,一起看看吧
    2016-09-09
  • 解決打包后出現(xiàn)錯(cuò)誤y.a.addRoute is not a function的問題

    解決打包后出現(xiàn)錯(cuò)誤y.a.addRoute is not a function的

    這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)

    記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)

    這篇文章主要介紹了記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 封裝一個(gè)更易用的Dialog組件過程詳解

    封裝一個(gè)更易用的Dialog組件過程詳解

    這篇文章主要為大家介紹了封裝一個(gè)更易用的Dialog組件過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解在vue開發(fā)中如何利用.env文件

    詳解在vue開發(fā)中如何利用.env文件

    我們?cè)?nbsp;vue 項(xiàng)目的目錄中經(jīng)??吹?nbsp;env 開頭的文件,在文件內(nèi)聲明一些變量,這些變量就是一些配置變量,在不同環(huán)境下可使用的變量,本文我們將給大家介紹在vue開發(fā)中如何利用.env文件,需要的朋友可以參考下
    2023-10-10
  • vue watch監(jiān)聽取不到this指向的數(shù)問題

    vue watch監(jiān)聽取不到this指向的數(shù)問題

    這篇文章主要介紹了vue watch監(jiān)聽取不到this指向的數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)

    mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)

    這篇文章主要介紹了mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • 在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解

    在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解

    在一次項(xiàng)目腳手架升級(jí)的過程中,將之前基于?webpack?搭建的項(xiàng)目移植到?Vite?構(gòu)建,這篇文章主要介紹了在?Vite項(xiàng)目中,使用插件?@rollup/plugin-inject?注入全局?jQuery,需要的朋友可以參考下
    2022-12-12
  • 關(guān)于Vue中axios的封裝實(shí)例詳解

    關(guān)于Vue中axios的封裝實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Vue中axios的封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Vue監(jiān)聽數(shù)據(jù)渲染DOM完以后執(zhí)行某個(gè)函數(shù)詳解

    Vue監(jiān)聽數(shù)據(jù)渲染DOM完以后執(zhí)行某個(gè)函數(shù)詳解

    今天小編就為大家分享一篇Vue監(jiān)聽數(shù)據(jù)渲染DOM完以后執(zhí)行某個(gè)函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評(píng)論

鹤峰县| 天柱县| 麻江县| 南平市| 五家渠市| 江安县| 雅安市| 许昌市| 永丰县| 长宁区| 文化| 察隅县| 香格里拉县| 巨鹿县| 乌恰县| 南澳县| 农安县| 桐梓县| 洞头县| 大理市| 禹州市| 高清| 连城县| 榆中县| 河源市| 柯坪县| 溧阳市| 噶尔县| 车险| 丹东市| 资兴市| 永川市| 宜君县| 司法| 大石桥市| 永川市| 象州县| 子长县| 瑞丽市| 达州市| 汶上县|