vuejs开发:vue-video-player插件小应用 - Web前端
作者:98wpeu发布时间:2026-09-07分类:网页前端技术浏览:2
导读:VueJs开发:vue-Video-player插件。最近的项目中需要实现视频断点续播的功能,一听到这个功能。内心方张了..但却又有点小窃喜,小懵乱。抱着求学态度去挑战一下。npm...

VueJs开发:vue-Video-player插件。
最近的项目中需要实现视频断点续播的功能,一听到这个功能。内心方张了..但却又有点小窃喜,小懵乱。抱着求学态度去挑战一下。
npm install VUE-video-player --save
import VideoPlayer from 'Vue-video-player'
reqUIre('video.JS/dist/video-js.CSS')
require('vue-video-player/src/custom-theme.css')
Vue.use(VideoPlayer)页面中应用。
<el-tree :data="Chapteroptions"
:props="defaultprops"
node-key='id'
highlight-current
:filter-node-method="filterNode"
ref="tree"
default-expand-all
@node-click="handleNodeClick" />
<video-player ref="videoPlayer"
class="video-player vjs-custom-skin"
style="width: 1000px;height: 576px;display: inline-flex"
:playsinline="true"
:options="playerOptions"
@pause="onPlayerPause($event)"
@ended="onPlayerEnded($event)"
@play="onPlayerPlay($event)"
@timeupdate="onPlayerTimeupDate($event)"
@ready="playerReadied"
/>
<script>
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import { treeselect } from "@/API//driver/videoChapter";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
Export default {
name: "videoPlayer",
components: { Treeselect,videoPlayer },
data() {
return {
//用户信息
User:{},
//===============================
paused: true,
learningDuration: {
userId: '', //用户id
chapterId:'',//章节id
timeLog: '', //视频观看时长
},
playerOptions: {
playbackRates: [0.5, 1.0, 1.5, 2.0], //播放速度
autoplay: false, // 如果true,浏览器准备好时开始回放。
muted: false, // 默认情况下将会消除任何音频。
loop: false, // 导致视频一结束就重新开始。
PReload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
language: 'zh-CN',
aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
sources: [
{
type: 'video/mp4', // 这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目
src: ''// url地址
}
],
hls: true,
poster: '', // 你的封面地址
width: document.documentElement.clientWidth, // 播放器宽度
notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
controlBar: {
//当前时间和持续时间的分隔符
timeDivider: true,
//显示持续时间
durationDisplay: true,
//是否显示剩余时间功能
remAIningTimeDisplay: false,
//全屏按钮
fullscreenToggle: true
}
}
};
},
computed: {
player() {
return this.$refs.videoPlayer.player//自定义播放
}
},
mounted () {
this.timer = SetInterval(this.putLearningObj, 3000)
},
destroyed () {
// 如果定时器在运行则关闭
if (this.timer) {
clearinterval(this.timer)
}
},
methods: {
//用户信息
getUser() {
getUserProfile().then(response => {
this.user = response.data;
this.learningDuration.userId = this.user.userId
});
},
//============================
fullScreen() {
const player = this.$refs.videoPlayer.player
player.requestFullscreen()//调用全屏api方法
player.isFullscreen(true)
player.play()
},
onPlayerPlay(player) {
this.paused = false
// player.play()
},
onPlayerPause (player) {
this.paused = true
// console.log('onPlayerPause!', player)
},
onPlayerEnded (player) {
this.paused = false;
// clearinterval(this.timer);
},
//当前播放位置发生变化时触发。
onPlayerTimeupdate (player) {
// console.log(' onPlayerTimeupdate!', this.timeLog)
},
/* 设置视频进度 */
playerReadIEd: function (player) {
},
};
</script>上面的 src视频地址可以换成具体的地址串,也能换成后台的地址串,因为我的是章节树所以我和章节id进行了关联。
/** 查询部门下拉树结构 */
getTreeselect() {
treeselect().then((response) => {
//封面
var img = '';
this.ChapterOptions = response.data;
for (let i = 0; i <this.ChapterOptions.length ; i++) {
this.videoName = this.ChapterOptions[0].children[0].chapterName
this.videoIntroduce = this.ChapterOptions[0].children[0].chapterIntroduce
this.VideoUrl = JSON.parse(this.ChapterOptions[0].children[0].videoAddress)
img = json.parse(this.ChapterOptions[0].children[0].imageAddress)
//初始化封面
for (let j = 0; j <img.length ; j++) {
this.playerOptions.poster =img[0];
}
//初始化第一个章节视频
for (let j = 0; j <this.VideoUrl.length ; j++) {
this.playerOptions.sources[0].src = this.VideoUrl[0]
}
//初始化章节
this.learningDuration.chapterId = this.ChapterOptions[0].children[0].id;
//默认高亮第一个章节节点
this.$nextTick(()=>{
this.$refs.tree.setCurrentKey(this.ChapterOptions[0].children[0].id);
})
}
});
},
// 筛选节点
filterNode(value, data) {
if (!value) return true;
return data.label.indexof(value) !== -1;
},
// 节点单击事件
handleNodeClick(data) {
// console.log(data)
var img = '';
//刷新原视频, 原封面
this.playerOptions.sources[0].src = '';
this.playerOptions.poster = '';
//转换视频
this.VideoUrl= JSON.parse(data.videoAddress);
// console.log("this.VideoUrl")
for (let i = 0; i <this.VideoUrl.length ; i++) {
this.playerOptions.sources[0].src = this.VideoUrl[0];
}
img = JSON.parse(data.imageAddress);
for (let i = 0; i <img.length ; i++) {
this.playerOptions.poster = img[0];
}
// console.log("this.playerOptions.sources[0].src")
// console.log(this.playerOptions.sources[0].src)
//章节介绍
this.videoIntroduce = data.chapterIntroduce;
//章节名称
this.videoName = data.chapterName;
//章节id
this.learningDuration.chapterId = data.id
// console.log(this.videoIntroduce)
},接下来就是 保存视频的进度条了,通过打印发现onPlayerTimeupdate可获取到视频的进度,故采用定时器 每3秒触发一次数据交互。
computed: {
player() {
return this.$refs.videoPlayer.player//自定义播放
}
},
mounted () {
this.timer = setInterval(this.putLearningObj, 3000)
},
destroyed () {
// 如果定时器在运行则关闭
if (this.timer) {
clearInterval(this.timer)
}
},
methods: {
putLearningObj () {
if (!this.paused) {
//保存视频进度
saveTime(this.learningDuration)
console.log('putLearningObj ~~~~~~~~~')
}
},
//当前播放位置发生变化时触发。
onPlayerTimeupdate (player) {
this.learningDuration.timeLog = player.CAChe_.currentTime
// console.log(' onPlayerTimeupdate!', this.timeLog)
},
},saveTime是我自定义的与后端交互的方法。(可自行定义)
// 保存视频进度
export function saveTime(data) {
return request({
url: '/***/****/***/',
method: 'put',
data: data
})
}那么到了这一步 进度就能保存下来了
进度恢复
想要恢复进度,就必须在视频播放前把 保存进度下来的设置到视频当中,通过打印可以看出playerReadied 可以设置
/* 设置视频进度 */
playerReadied: function (player) {
//可在此调用后台交互方法
...
player.currentTime(this.learningDuration.timeLog)
},
相关推荐
- 什么是JavaScript国际化API(I18n)? - Web前端
- 谷歌表示,Windows 10上的Chrome现在将使用更少的内存 - Web前端
- VUE开发:vue-cli中实现响应式布局 - Web前端
- vue开发TIPS:vue-cli中实现响应式布局 - Web前端
- Microsoft Teams显示屏获得了一组便捷的新功能 - Web前端
- Windows 10:20H2预览删除了旧的Edge,21H1获得了错误的打印机修复 - Web前端
- Web前端开发:无服务器功能及其部署指南 - Web前端
- Web API 接口参考Console:Console.countReset() - Web前端
- React中功能组件和类组件之间的区别 - Web前端
- 使用React Hooks和TypeScript - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


