vue2.0+vue-dplayer实现hls播放的示例
作者:Fei___ 时间:2024-05-29 22:46:56
起因
之前写了一篇《 vue2.0+vue-video-player实现hls播放》,里边有提到在用vue-video-player之前,我尝试着使用vue-dplayer实现hls播放,但是当时时间紧迫,还没有完成,就换方案了。现在抽时间把它补齐吧。
开始
安装依赖
npm install vue-dplayer -S
编写组件HelloWorld.vue
<template>
<div class="hello">
<d-player ref="player" @play="play" :video="video" :contextmenu="contextmenu"></d-player>
</div>
</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>
<div id="app"></div>
<!-- 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>
<div class="dplayer"></div>
</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';
实现播放
最后
github地址:https://github.com/PhillCheng/vue-dplayer-hls
来源:http://blog.csdn.net/fei565789229/article/details/78933724
标签:vue,video,player,hls
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL SERVER 2005数据库还原的方法
2024-01-26 12:28:57
![](https://img.aspxhome.com/file/2023/8/102068_0s.jpg)
JavaScript中诡异的delete操作符
2024-04-10 16:15:33
python用selenium打开chrome浏览器保持登录方式
2022-02-19 10:28:08
![](https://img.aspxhome.com/file/2023/6/133306_0s.jpg)
Python数据分析matplotlib设置多个子图的间距方法
2021-03-16 01:08:29
SQLSERVER数据库备份后无法还原的解决办法
2024-01-21 14:05:01
Python中使用logging模块打印log日志详解
2021-10-01 02:32:17
Python 实现PS滤镜中的径向模糊特效
2023-11-04 19:48:49
![](https://img.aspxhome.com/file/2023/6/80696_0s.png)
Python文件基本操作实用指南
2022-05-05 23:22:28
![](https://img.aspxhome.com/file/2023/1/112151_0s.png)
js不是基础的基础
2024-05-03 15:57:54
mysql中截取字符串的6个函数讲解
2024-01-13 13:54:11
![](https://img.aspxhome.com/file/2023/9/118159_0s.png)
一张图告诉你计算机编程语言的发展历史
2023-03-29 15:16:42
![](https://img.aspxhome.com/file/2023/3/131833_0s.jpg)
python paramiko远程服务器终端操作过程解析
2022-10-08 00:50:14
python3之Splash的具体使用
2023-06-27 05:26:06
![](https://img.aspxhome.com/file/2023/5/131705_0s.png)
SQL Server 数据库优化
2024-01-20 00:34:18
python 监听salt job状态,并任务数据推送到redis中的方法
2022-09-14 05:19:47
Python全栈之队列详解
2022-10-12 07:51:33
![](https://img.aspxhome.com/file/2023/5/85385_0s.png)
vs10安装之后一些列问题
2024-01-29 11:59:48
SQL Server 磁盘请求超时的833错误原因及解决方法
2024-01-14 00:14:43
![](https://img.aspxhome.com/file/2023/4/104104_0s.png)
使用python模拟高斯分布例子
2021-04-27 05:22:22
![](https://img.aspxhome.com/file/2023/9/118359_0s.jpg)
pytorch中nn.RNN()汇总
2022-08-31 03:11:34