您好,欢迎访问一九零五行业门户网

vue-dplayer实现hls播放的步奏详解

这次给大家带来vue-dplayer实现hls播放的步奏详解,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 './vuedplayerhls'; export default {  name: 'helloworld',  data () {   return {    msg: 'welcome to your vue.js app',    video: {      url: 'https://logos-channel.scaleengine.net/logos-channel/live/biblescreen-ad-free/chunklist_w630020335.m3u8',      pic: 'http://static.smartisanos.cn/pr/img/video/video_03_cc87ce5bdb.jpg',      type: 'hls'     },     autoplay: false,     player: null,     contextmenu: [       {         text: 'github',         link: 'https://github.com/moeplayer/vue-dplayer'       }     ]   }  },  components: {   'd-player': vuedplayer  },  methods: {   play() {     console.log('play callback')    }  },  mounted() {   this.player = this.$refs.player.dp;   // console.log(this.player);   var hls = new hls();   hls.loadsource('https://logos-channel.scaleengine.net/logos-channel/live/biblescreen-ad-free/chunklist_w630020335.m3u8');   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”,但是我修改之后发现无法播放。于是去看了源码,发现源码内有这样一处:
也就是说不论你在自己的组件内填写的是什么,其实都是使用的'normal'来new的dplayer实例。
修改源码
自定义一个组件vuedplayerhls.vue,然后copy源代码,问题处修改为: type: this.video.type
<template>  <p class="dplayer"></p> </template> <script>  require('../../node_modules/dplayer/dist/dplayer.min.css');  import dplayer from 'dplayer'  export default {   props: {    autoplay: {     type: boolean,     default: false    },    theme: {     type: string,     default: '#fadfa3'    },    loop: {     type: boolean,     default: true    },    lang: {     type: string,     default: 'zh'    },    screenshot: {     type: boolean,     default: false    },    hotkey: {     type: boolean,     default: true    },    preload: {     type: string,     default: 'auto'    },    contextmenu: {     type: array    },    logo: {     type: string    },    video: {     type: object,     required: true,     validator(value) {      return typeof value.url === 'string'     }    }   },   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('play', () => {     this.$emit('play')    })    player.on('pause', () => {     this.$emit('pause')    })    player.on('canplay', () => {     this.$emit('canplay')    })    player.on('playing', () => {     this.$emit('playing')    })    player.on('ended', () => {     this.$emit('ended')    })    player.on('error', () => {     this.$emit('error')    })   }  } </script>
在原组件(helloworld.vue)内import新组件
import vuedplayer from './vuedplayerhls';
实现播放
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ajax动态赋值数据图
react组件的性能优化有哪些方面
以上就是vue-dplayer实现hls播放的步奏详解的详细内容。
其它类似信息

推荐信息