这篇文章主要介绍了vue-video-player 断点续播的实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
正文
vue-video-player 断点续播的实现
这篇文章主要介绍了vue-video-player 断点续播的实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
最近的项目中需要实现视频断点续播的功能,一听到这个功能。内心方张了..但却又有点小窃喜,小懵乱。抱着求学态度去挑战一下。
1.安装插件
|
1
|
npm install vue-video-player --save |
2.Main.js 引入组件
|
1
2
3
4
|
import VideoPlayer from 'vue-video-player'require('video.js/dist/video-js.css')require('vue-video-player/src/custom-theme.css')Vue.use(VideoPlayer) |
3.页面使用组件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<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进行了关联
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/** 查询部门下拉树结构 */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秒触发一次数据交互
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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是我自定义的与后端交互的方法。(可自行定义)
|
1
2
3
4
5
6
7
8
|
// 保存视频进度export function saveTime(data) { return request({ url: '/***/****/***/', method: 'put', data: data })} |
那么到了这一步 进度就能保存下来了
4.进度恢复
想要恢复进度,就必须在视频播放前把 保存进度下来的设置到视频当中,通过打印可以看出playerReadied 可以设置
|
1
2
3
4
5
6
|
/* 设置视频进度 */playerReadied: function (player) {//可在此调用后台交互方法...player.currentTime(this.learningDuration.timeLog)}, |
到此 进度可以 恢复了 大功告成!。至于后台交互数据 需求不一样,代码也就没有贴出来。

到此这篇关于vue-video-player 断点续播的文章就介绍到这了,更多相关vue video player 断点续播内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://blog.csdn.net/weixin_45676325/article/details/113371747

发表评论