vue2.0+vue-dplayer テクノロジーを使用して hls 再生を実装する方法の例

亚连
リリース: 2018-06-02 09:27:53
オリジナル
3013 人が閲覧しました

この記事では主に hls 再生を実装するための vue2.0+vue-dplayer の例を紹介し、参考として提供します。

原因

以前「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 のデモ ページのコードによると、hls をサポートしたい場合は、video.type を "hls" に設定する必要がありますが、それを変更した後、再生できなかったということ。そこでソース コードを調べたところ、ソース コード内に次の場所が見つかりました。

vue2.0+vue-dplayer テクノロジーを使用して hls 再生を実装する方法の例

つまり、コンポーネントに何を入力しても、実際には新しい Dplayer インスタンスを作成するために「normal」を使用します。

ソース コードを変更する

コンポーネント VueDPlayerHls.vue をカスタマイズし、ソース コードをコピーして、問題を次のように変更します。 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 VueDPlayer from &#39;./VueDPlayerHls&#39;;
ログイン後にコピー

再生を実現

vue2.0+vue-dplayer テクノロジーを使用して hls 再生を実装する方法の例

以上、皆さんの参考になれば幸いです。

関連記事:

vueでv-forを使用すると赤色と警告が表示される問題を解決する方法(詳細チュートリアル)

Vuejsで検索マッチング関数メソッドを実装する方法(詳細チュートリアル)

選択ドロップダウン ボックスを使用して、vue.js でバインドと値のメソッドを実装します

以上がvue2.0+vue-dplayer テクノロジーを使用して hls 再生を実装する方法の例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!