uniapp计算视频学习进程,并且下次回来继续播放(不能快进)

前言:

该功能分别有三个难点:

1.计算百分比,计算上次播放秒数

2.如何使视频无法快进

3.如何从上次播放描述开始

首先现在这里熟悉一下如何计算:

1.计算视频播放的百分比

比如该视频的总时长为120秒,然后现在播放的时长为12秒,计算当前视频学习时长的百分比

javascript 复制代码
let a = 120//总时长
let b = 12//现在播放的时长
let c = b / a * 100//总进度  10%

2.计算上次播放视频的秒数

比如该视频的总时长为120秒,当前视频学习时长为10%,计算上次播放视频的秒数

javascript 复制代码
let a = 120//总时长
let c = 10//百分比
let b = a * (c / 100)//上次播放时长

到这里第一个难题已经解决

然后想要获取上次播放视频的秒数最佳方法就是请求接口了

请求接口这一步就省去了,不懂得在评论区留言

其次、如何让视频无法快进

上代码:

html:

html 复制代码
<video id="myVideo" :title="data.course_title"  :initial-time="videoContext" style="width: 100%;" :src="data.video_url" controls @timeupdate="videoTimeUpdateEvent"></video>

js:

javascript 复制代码
currentTime: '', //现在的时长
durationTime: '', //总时长
videoContext: 0,
watch: 0, //用来判断是否快进
box: null,//绑定上次文
progress: ''//百分比
javascript 复制代码
            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
				}
			},

到这里第二个问题就解决了

最后,使视频从上次播放秒数开始

javascript 复制代码
                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 

整体代码如下:

html 复制代码
<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
				}
			},

到这里就结束了,最后希望能帮助到大家,谢谢支持!

相关推荐
艾莉丝努力练剑8 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
人生游戏牛马NPC1号2 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
林林要一直努力6 小时前
AOSP Settings模块问题初窥
android·学习·bug·android studio
余大侠在劈柴9 小时前
pdf.js 开发指南:在 Web 项目中集成 PDF 预览功能
前端·javascript·学习·pdf
有谁看见我的剑了?11 小时前
iperf3 网络带宽测试工具学习
学习·测试工具
老神在在00111 小时前
SpringMVC2
java·前端·学习·spring·java-ee
老神在在00111 小时前
SpringMVC3
java·前端·学习·spring·java-ee
SY.ZHOU13 小时前
H264的NALU结构
音视频·视频编解码
FinelyYang14 小时前
uniapp video视频全屏播放后退出,页面字体变大,样式混乱问题
前端·javascript·uni-app