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
				}
			},

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

相关推荐
理想国的女研究僧19 分钟前
Jupyter Notebook操作指南(1)
ide·python·学习·jupyter
百思可瑞教育31 分钟前
Vue 生命周期详解:从初始化到销毁的全过程剖析
前端·javascript·vue.js·前端框架·uni-app·北京百思可瑞教育·百思可瑞教育
GilgameshJSS1 小时前
【学习K230-例程19】GT6700-TCP-Client
网络·python·网络协议·学习·tcp/ip
悠悠~飘1 小时前
php学习(第五天)
学习·php
能不能别报错1 小时前
K8s学习笔记(二):Pod
笔记·学习·kubernetes
呱呱巨基2 小时前
C/C++ 内存管理
c++·笔记·学习
jingling5553 小时前
uniapp | 快速上手ThorUI组件
前端·笔记·前端框架·uni-app
楼田莉子3 小时前
python小项目——学生管理系统
开发语言·python·学习
在路上`3 小时前
前端学习之后端java小白(二)-sql约束/建表
java·sql·学习