该功能分别有三个难点:
比如该视频的总时长为120秒,然后现在播放的时长为12秒,计算当前视频学习时长的百分比
let a = 120//总时长
let b = 12//现在播放的时长
let c = b / a * 100//总进度 10%
比如该视频的总时长为120秒,当前视频学习时长为10%,计算上次播放视频的秒数
let a = 120//总时长
let c = 10//百分比
let b = a * (c / 100)//上次播放时长
到这里第一个难题已经解决
然后想要获取上次播放视频的秒数最佳方法就是请求接口了
请求接口这一步就省去了,不懂得在评论区留言
上代码:
html:
<video id="myVideo" :title="data.course_title" :initial-time="videoContext" style="width: 100%;" :src="data.video_url" controls @timeupdate="videoTimeUpdateEvent"></video>
js:
currentTime: '', //现在的时长
durationTime: '', //总时长
videoContext: 0,
watch: 0, //用来判断是否快进
box: null,//绑定上次文
progress: ''//百分比
onReady() {
this.box = uni.createVideoContext('myVideo')
},
videoTimeUpdateEvent(e) {
this.currentTime = Number(e.detail.currentTime);
this.durationTime = Number(e.detail.duration);
if (this.currentTime - this.watch > 10) {
uni.showToast({
title: '不能快进',
icon: 'none'
})
this.box.seek(this.watch)
this.videoContext = this.watch
} else {
this.watch = this.currentTime
}
},
到这里第二个问题就解决了
if (e.detail.currentTime <= 1) {
this.box.seek(e.detail.duration * (this.progress / 100))
this.videoContext = e.detail.duration * (this.progress / 100)
this.watch = e.detail.duration * (this.progress / 100)
} else
<video id="myVideo" :title="data.course_title" :initial-time="videoContext" style="width: 100%;" :src="data.video_url" controls @timeupdate="videoTimeUpdateEvent"></video>
currentTime: '', //现在的时长
durationTime: '', //总时长
videoContext: 0,
watch: 0, //用来判断是否快进
box: null,//绑定上次文
progress: ''//百分比
onReady() {
this.box = uni.createVideoContext('myVideo')
},
videoTimeUpdateEvent(e) {
this.currentTime = Number(e.detail.currentTime);
this.durationTime = Number(e.detail.duration);
if (e.detail.currentTime <= 1) {
this.box.seek(e.detail.duration * (this.progress / 100))
this.videoContext = e.detail.duration * (this.progress / 100)
this.watch = e.detail.duration * (this.progress / 100)
} else if (this.currentTime - this.watch > 10) {
uni.showToast({
title: '不能快进',
icon: 'none'
})
this.box.seek(this.watch)
this.videoContext = this.watch
} else {
this.watch = this.currentTime
}
},
到这里就结束了,最后希望能帮助到大家,谢谢支持!