(编辑:jimmy 日期: 2024/12/22 浏览:2)
最近的项目中需要实现视频断点续播的功能,一听到这个功能。内心方张了..但却又有点小窃喜,小懵乱。抱着求学态度去挑战一下。
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) },
4.进度保存
接下来就是 保存视频的进度条了,通过打印发现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 }) }
那么到了这一步 进度就能保存下来了
4.进度恢复
想要恢复进度,就必须在视频播放前把 保存进度下来的设置到视频当中,通过打印可以看出playerReadied 可以设置
/* 设置视频进度 */ playerReadied: function (player) { //可在此调用后台交互方法 ... player.currentTime(this.learningDuration.timeLog) },
到此 进度可以 恢复了 大功告成!。至于后台交互数据 需求不一样,代码也就没有贴出来。