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

前言:

该功能分别有三个难点:

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

2.如何使视频无法快进

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

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

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

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

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

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

比如该视频的总时长为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
				}
			},

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

  • 43
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechWhiz-晓同

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值