vue2.0+vue-dplayer实现hls播放的示例
作者:Fei___ 发布时间:2024-05-29 22:46:56
标签:vue,video,player,hls
起因
之前写了一篇《 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


猜你喜欢
- python 根据正则表达式提取指定的内容正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。 下面演
- 配置类config_file:from configparser import ConfigParserclass config_file:
- 方法一:使用列表推导式>>> vec = [[1,2,3],[4,5,6],[7,8,9]]>>> ge
- 1 、据说python3就没有这个问题了2 、u'字符串' 代表是unicode格式的数据,路径最好写成这个格式,别直接跟字
- 在我们制作网页的时候会经常碰到一些需求,如果不知道方法,说不定会困扰我们半天。其实实现它们都很简单,下面我们就一起来看看这些常用的网页编辑方
- 如果直接执行SQL语句或者参数绑定则不用担心太多,如以下ORACLE存储过程 create or replace&nbs
- 当鼠标移动上去后,字慢慢的变大的 效果应该 如果实现啊<!DOCTYPE html PUBLIC "-//W3C//DTD
- 这是一个适合移动设备WEB应用的日期和时间拾取器,在桌面版的日期拾取器我们一般用jQuery UI的datepicker插件,而移动手机版的
- 目录一、使用JDBC连接数据库1.使用JDBC-ODBC桥驱动程序连接数据库2.下面进行代码演示3.注意点二、源码:一、使用JDBC连接数据
- 列表 List列表是任意对象的集合,在 Python 中通过逗号分隔的对象序列括在方括号 ( [] ) 中people_list = [
- 本文实例讲述了python 正则表达式贪婪模式与非贪婪模式原理、用法。分享给大家供大家参考,具体如下:之前未接触过正则表达式,今日看pyth
- <?php function genpage(&$sql,$page_size=10) { global $pages,$su
- 如何使用migrations的使用非常简单: 修改model, 比如增加field, 然后运行python manager.py makem
- 如果你现在正在使用Restful API,并且你需要通过web项目来构建json格式字符串的响应,那么这篇文章将帮助你使用javascrip
- 背景:我司Redis服务器使用的亚马逊服务,本地需要通过跳板机,然后才有权限访问Redis服务。连接原理:使用SSHTunnelForwar
- js的eval代码快速解密有一段js代码内容如下:eval(function(E,I,A,D,J,K,L,H){function C(A)后
- 准备工作我们需要把秒杀的商品加入购物车,因为脚本点击的是全选,所以不需要的商品要移出购物车。过程分析1.打开某宝网站;pq = webdri
- 看看怎样抓到你:<%Dim objCMFUDim strModifiedSet objCMFU 
- 一、表单验证form1、创建一个新的表单:<form id="id是唯一的,不可重复" name=“可重复”,me
- 经常在网上冲浪的朋友是否曾注意到有些网站的鼠标不是规则的斜向上箭头的形状,而是"十"