首頁 > web前端 > js教程 > 主體

使用vue2.0+vue-dplayer這些技術如何實現hls播放的範例

亚连
發布: 2018-06-02 09:27:53
原創
3021 人瀏覽過

這篇文章主要介紹了vue2.0 vue-dplayer實作hls播放的範例,現在分享給大家,也給大家做個參考。

起因

之前寫了一篇《 vue2.0 vue-video-player實作hls播放》,裡邊有提到在用vue-video-player之前,我嘗試著使用vue-dplayer實現hls播放,但是當時時間緊迫,還沒完成,就換方案了。現在抽空把它補齊吧。

開始

安裝相依

npm install vue-dplayer -S
登入後複製

寫元件HelloWorld.vue

<template>
 <p class="hello">
  <d-player ref="player" @play="play" :video="video" :contextmenu="contextmenu"></d-player>
 </p>
</template>

<script>
import VueDPlayer from &#39;./VueDPlayerHls&#39;;
export default {
 name: &#39;HelloWorld&#39;,
 data () {
  return {
   msg: &#39;Welcome to Your Vue.js App&#39;,
   video: {
     url: &#39;https://logos-channel.scaleengine.net/logos-channel/live/biblescreen-ad-free/chunklist_w630020335.m3u8&#39;,
     pic: &#39;http://static.smartisanos.cn/pr/img/video/video_03_cc87ce5bdb.jpg&#39;,
     type: &#39;hls&#39;
    },
    autoplay: false,
    player: null,
    contextmenu: [
      {
        text: &#39;GitHub&#39;,
        link: &#39;https://github.com/MoePlayer/vue-dplayer&#39;
      }
    ]
  }
 },
 components: {
  &#39;d-player&#39;: VueDPlayer
 },
 methods: {
  play() {
    console.log(&#39;play callback&#39;)
   }
 },
 mounted() {
  this.player = this.$refs.player.dp;
  // console.log(this.player);
  var hls = new Hls();
  hls.loadSource(&#39;https://logos-channel.scaleengine.net/logos-channel/live/biblescreen-ad-free/chunklist_w630020335.m3u8&#39;);
  hls.attachMedia(this.player);
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
登入後複製

引入hls.js

本來是使用import引入,可是執行報錯。所以就先在index.html內用script標籤引進來。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>vue-dplayer-hls</title>
 </head>
 <body>
  <p id="app"></p>
  <!-- built files will be auto injected -->
  <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
 </body>
</html>
登入後複製

注意:

根據DPlayer Demo頁面程式碼,想支援hls,需要將video. type 設定為”hls”,但是我修改之後發現無法播放。於是去看了源碼,發現源碼內有這樣一處:

使用vue2.0+vue-dplayer這些技術如何實現hls播放的範例

#也就是說你在自己的元件內填寫的是什麼,其實都是使用的'normal'來new的Dplayer實例。

修改原始碼

自訂一個元件VueDPlayerHls.vue,然後 copy原始碼,問題處修改為: type: this.video.type

#
<template>
 <p class="dplayer"></p>
</template>

<script>
 require(&#39;../../node_modules/dplayer/dist/DPlayer.min.css&#39;);
 import DPlayer from &#39;DPlayer&#39;
 export default {
  props: {
   autoplay: {
    type: Boolean,
    default: false
   },
   theme: {
    type: String,
    default: &#39;#FADFA3&#39;
   },
   loop: {
    type: Boolean,
    default: true
   },
   lang: {
    type: String,
    default: &#39;zh&#39;
   },
   screenshot: {
    type: Boolean,
    default: false
   },
   hotkey: {
    type: Boolean,
    default: true
   },
   preload: {
    type: String,
    default: &#39;auto&#39;
   },
   contextmenu: {
    type: Array
   },
   logo: {
    type: String
   },
   video: {
    type: Object,
    required: true,
    validator(value) {
     return typeof value.url === &#39;string&#39;
    }
   }
  },
  data() {
   return {
    dp: null
   }
  },
  mounted() {
   const player = this.dp = new DPlayer({
    element: this.$el,
    autoplay: this.autoplay,
    theme: this.theme,
    loop: this.loop,
    lang: this.lang,
    screenshot: this.screenshot,
    hotkey: this.hotkey,
    preload: this.preload,
    contextmenu: this.contextmenu,
    logo: this.logo,
    video: {
     url: this.video.url,
     pic: this.video.pic,
     type: this.video.type
    }
   })
   player.on(&#39;play&#39;, () => {
    this.$emit(&#39;play&#39;)
   })
   player.on(&#39;pause&#39;, () => {
    this.$emit(&#39;pause&#39;)
   })
   player.on(&#39;canplay&#39;, () => {
    this.$emit(&#39;canplay&#39;)
   })
   player.on(&#39;playing&#39;, () => {
    this.$emit(&#39;playing&#39;)
   })
   player.on(&#39;ended&#39;, () => {
    this.$emit(&#39;ended&#39;)
   })
   player.on(&#39;error&#39;, () => {
    this.$emit(&#39;error&#39;)
   })
  }
 }
</script>
登入後複製

在原始元件(HelloWorld.vue)內import新元件

import VueDPlayer from &#39;./VueDPlayerHls&#39;;
登入後複製

實作播放

使用vue2.0+vue-dplayer這些技術如何實現hls播放的範例

#上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

在vue中如何解決v-for使用報紅並出現警告的問題(詳細教學)

在Vuejs中如何實作搜尋比對功能方法(詳細教學)

#在vue.js中利用select下拉方塊實作綁定和取值方法

#

以上是使用vue2.0+vue-dplayer這些技術如何實現hls播放的範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!