dplayer安装php_vue-dplayer实现hls播放的步奏详解

本文详细介绍了如何在Vue项目中使用vue-dplayer组件实现hls播放,包括安装依赖、编写组件、引入hls.js以及解决播放问题的过程。通过修改源码以支持hls类型,并提供了一个完整的实战案例。
摘要由CSDN通过智能技术生成

这次给大家带来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

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);

}

}

引入hls.js

本来是使用import引入,可是执行报错。所以就先在index.html内用script标签引入进来。

vue-dplayer-hls

注意:

根据DPlayer Demo页面代码,想支持hls,需要将video.type 设置为”hls”,但是我修改之后发现无法播放。于是去看了源码,发现源码内有这样一处:

173565e7e3.png

也就是说不论你在自己的组件内填写的是什么,其实都是使用的'normal'来new的Dplayer实例。

修改源码

自定义一个组件VueDPlayerHls.vue,然后copy源代码,问题处修改为: type: this.video.type

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')

})

}

}

在原组件(HelloWorld.vue)内import新组件import VueDPlayer from './VueDPlayerHls';

实现播放

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值