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

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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 08:00:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 08:00:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 08:00:03       87 阅读
  4. Python语言-面向对象

    2023-12-15 08:00:03       96 阅读

热门阅读

  1. 在Go中理解栈和先进先出原则

    2023-12-15 08:00:03       59 阅读
  2. 【C语言】clock_gettime函数的使用

    2023-12-15 08:00:03       70 阅读
  3. 盈科数智视频管理平台简介

    2023-12-15 08:00:03       55 阅读
  4. 代码随想录494.目标和

    2023-12-15 08:00:03       45 阅读
  5. 模拟I2C通信

    2023-12-15 08:00:03       52 阅读
  6. npm 和 pip 、cnpm、Yum分别是什么

    2023-12-15 08:00:03       70 阅读
  7. Crow:基于req.rul查找路由Rule对象及匹配参数

    2023-12-15 08:00:03       62 阅读
  8. Android Studio(Flutter)常用快捷键

    2023-12-15 08:00:03       50 阅读
  9. GitHub 深度解析:高级功能和最佳实践

    2023-12-15 08:00:03       56 阅读
  10. uniapp使用u-search以及相关api

    2023-12-15 08:00:03       56 阅读
  11. docker容器引擎

    2023-12-15 08:00:03       44 阅读
  12. KVO(键值观察)

    2023-12-15 08:00:03       69 阅读
  13. Visual Studio(VS)常用快捷键(最详细)

    2023-12-15 08:00:03       47 阅读