赞
踩
npm i dplayer -S // 视频播放器插件
npm i hls.js -S // 播放hls流插件
dplayer 官网: http://dplayer.js.org/zh/guide.html
import "video.js/dist/video-js.css";
import Hls from "hls.js";
import DPlayer from "dplayer";
<template>
<div class="myVideo">
<div id="dplayer" ref="player" class="dplayer video-box"></div>
</div>
</template>
<script>
import "video.js/dist/video-js.css";
import Hls from "hls.js";
import DPlayer from "dplayer";
/*
npm i dplayer -S // 视频播放器插件
npm i hls.js -S // 播放hls流插件
*/
export default {
data() {
return {
dp: null,
videoInfo: {
img: "https://cn.bing.com/th?id=OHR.MeotoIwa_ZH-CN3126370410_1920x1080.jpg&rf=LaDigue_1920x1080.jpg", // 视频封面
url: "https://e-sign.dms.t.cn-np.com/files/m3u8_file/c4b94118-3c8d-4410-9987-985c2b44c278/c4b94118-3c8d-4410-9987-985c2b44c278.m3u8", // 播放视频的路径'
},
};
},
created() {
this.$nextTick(() => {
this.loadVideo(this.videoInfo);
});
},
methods: {
loadVideo(videoInfo) {
this.dp = new DPlayer({
element: this.$refs.player,
loop: false,
video: {
pic: videoInfo.img, // 封面
url: videoInfo.url,
type: "customHls",
customType: {
customHls: function (video, player) {
const hls = new Hls();
hls.loadSource(video.src);
hls.attachMedia(video);
},
},
},
});
},
},
};
</script>
<style lang="less" scoped>
.video-box {
width: 100%;
max-width: 500px;
max-height: 500px;
}
</style>
mounted() {
// 禁用视频右键
document.querySelector("#dplayer").oncontextmenu = () => {
document.querySelector(".dplayer-menu").style.display = "none";
document.querySelector(".dplayer-mask").style.display = "none";
return false;
};
},
<style lang="less" scoped>
// 禁用视频右键
::v-deep .dplayer-menu,.dplayer-mask {
display: none !important;
}
</style>
播放器在拿到XXX.m3u8视频地址(索引文件)后,会去请求拿到分片数据资源,也就是一大串的.ts文件列表,然后依次去请求这些.ts文件(当然这些是播放器内部自己实现的)
vue 实现.m3u8格式视频播放的方法有很多,可以看我上两篇博客,都有贴代码实现
1、原生videojs : vue使用原生videojs 播放m3u8格式的视频
2、vue-dplayer : vue使用vue-dplayer 播放m3u8格式的视频
3、dplayer : dplayer 播放m3u8格式的视频
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。