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

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

相关推荐
豆沙沙包?1 小时前
5.学习笔记-SpringMVC(P61-P70)
数据库·笔记·学习
每次的天空3 小时前
Android学习总结之Room篇
android·学习·oracle
BO_S__4 小时前
python调用ffmpeg对截取视频片段,可批量处理
python·ffmpeg·音视频
亦双城的双子娴4 小时前
通过音频的pcm数据格式利用canvas绘制音频波形图
音视频·pcm·canva可画
Nuyoah.4 小时前
《Vue3学习手记5》
前端·javascript·学习
陶然同学7 小时前
RabbitMQ全栈实践手册:从零搭建消息中间件到SpringAMQP高阶玩法
java·分布式·学习·rabbitmq·mq
欧先生^_^8 小时前
学习 Apache Kafka
学习·kafka·apache
Antonio9159 小时前
【音视频】⾳频处理基本概念及⾳频重采样
ffmpeg·音视频·aac
妙极矣9 小时前
JAVAEE初阶01
java·学习·java-ee
娃娃略9 小时前
【AI模型学习】双流网络——更强大的网络设计
网络·人工智能·pytorch·python·神经网络·学习