Uniapp video timeupdate计时器代替@ended提交视频进度

Uniapp viedo timeupdate计时器代替@ended提交视频进度与视频拖动限制

:由于uniapp 在Hbuilder打包后可能导致viedo组件@ended无法触发的问题,不能完成视频。
:除此之外,还有可能需要的视频未完整播放不可拖动功能,但是已经播放过的视频可以随意拖动

Template:

<template>
    <view>
        <view class="uni-padding-wrap uni-common-mt">
            <view>
                <video id="myVideo" src=""
                 @error="videoErrorCallback"
                 :danmu-list="danmuList" 
                 enable-danmu 
                 danmu-btn 
                 controls
                 enable-progress-gesture
                 show-progress
                 @timeupdate="timeupdate"
                 @ended="onfinish"
                 >
                </video>
            </view>
        </view>
    </view>
</template>

JS

<script>
	export default {
		data() {
			return {
				initialTime: 0, //用户视频播放的位置、
				videoTime: 0, // 视频时间长度
				watchTime: 0, // 实际观看时间
				videoRealTime: 0, // 实时播放进度
				isFinishedVideo: false, //该值在视频播放完成时由你的接口保存,并在视频切换时拿取这个值判断是否限制拖动
				video_timer_flag: true, //控制节流器
			}
		},

		onLoad() {
			this.videoContext = uni.createVideoContext('myVideo', this);
			/**
				这里需要获取视频播放信息,其中需要视频播放进度状态 完成/未完成 字段来改变this.isFinishedVideo是否做限制
			**/
		},

		//当播放到末尾时或用户手动拖到末尾时触发
		onfinish(e) {
			//console.log("观看时长", this.watchTime);
			//console.log("视频时长", this.videoTime);
			/* 不限制拖动 已播放视频可以拖动进度*/
			if (this.isFinishedVideo) {
				//不限制拖动时的操作
			} else {
				console.log("不可拖动进度完成");
			}

			/* ios触发条件 ios没有出现@ended无法触发的情况 根据自己需求调整 */
			// if (uni.getSystemInfoSync().platform == "ios" && this.videoTime != 0 && this.watchTime != 0) {
			// 	if (this.videoTime - this.watchTime <= 1) { //差值的大小由你的业务需要调整
			// 		this.finshiThisVideo();//调用完成视频进度
			// 		this.watchTime = 0;//重置观看时间
			// 	} else {
			// 		console.log("ios不满足完成条件");
			// 		this.videoContext.play()
			// 	}
			// }

			/* 安卓触发条件 这里是阻止拖动完成,实际使用timeupdate()(安卓不依赖@ended,有几率不触发) */
			if (uni.getSystemInfoSync().platform == "android" && this.videoTime != 0 && this.watchTime != 0) {
				let a = this.videoTime;
				let b = this.watchTime;
				let c = a - b;
				if (c < 1) {
					//这里不依赖@ended完成视频进度的提交
				} else {
					console.log("安卓不满足完成条件");
					this.videoContext.play()//重新播放或seek到最后播放的位置
				}
			}
		},
		//计时器
		timeupdate(e) {
			//当前进度
			let currentTime = parseInt(e.detail.currentTime);
			//总进度
			let duration = parseInt(e.detail.duration);
			// console.log('当前进度:',currentTime,'总进度:', duration);
			
			/* 这里是记录播放时长以判定用户拖动 */
			//前置判断 若限制拖动
			if (this.isShowProgress == false) {
				this.videoTime = parseInt(duration)
				// 记录当前视频进度
				var skipTime = parseInt(e.detail.currentTime)
				if (skipTime - this.watchTime > 3) {
					//跳过进度大于3则跳回实际观看时间点
					this.videoContext.seek(this.watchTime)
					if (this.watchTime != 0) {
						//首次观看不提醒
						uni.showToast({
							title: '未完整观看视频,不可跳过',
							icon: 'none',
							duration: 2000
						})
					}
				} else {
					//没有跳过行为保存观看时长
					this.videoRealTime = parseInt(e.detail.currentTime)
					if (this.videoRealTime > this.watchTime) {
						this.watchTime = this.videoRealTime
					}
				}
			} else {
				// console.log("可拖动进度条");
			}
			
			/* 安卓提交进度 */
			if (uni.getSystemInfoSync().platform == "android") {
				//实际观看不为0
				if (this.watchTime != 0 && this.videoTime != 0) {
					//播放到末尾 
					if (this.videoTime - this.watchTime <= 1) {
						this.video_ended();//调用节流器
						this.videoContext.pause()//暂停当前视频播放
						this.watchTime = 0;//重置时长
						console.log("安卓完成播放");
					}
				}
			}

		},
		/* 完成视频播放进度 节流是由于 timeupdate在2秒内可触发4次 */
		video_ended() {
			let self = this;
			if (!this.video_timer_flag) {
				return
			}
			this.video_timer_flag = false
			setTimeout(function() {
				console.log("安卓触发节流器");
				self.finshiThisVideo();//这里是完成视频播放后的业务
				self.video_timer_flag = true;
			}, 1000 * 2.5);//这里大于2s即可
		},
	}
</script>
	以上是计时器替代@ended,及如何限制用户提前完成视频的方法。  没有最完美的代码,欢迎提出改进意见。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
这是一个灵活的视频播放器。 MediaPlayer与VideoView完全分开,可以替换为其他播放器内核,如ExoPlayer和ijkPlayer。 可以完全自定义播放器视图,我们称之为控制面板。 此外,可以使用MediaPlayerManager来控制播放行为,例如全屏模式,小屏幕模式以及RecyclerView中的智能匹配模式。Features全屏,小屏播放内部支持RecyclerView中播放自定义UIAPP内全局播放静音循环播放手势操作(小窗:单指拖动,双指缩放;全屏:音量,亮度,快进)ijkPlayer支持ExoPlayer支持重力感应支持PreviewDownloadDemo DownloadGetting startedbuild.gradledependencies {     // required     implementation 'org.salient.artvideoplayer:artplayer-java:0.6.0'     // Default control panel: optional     implementation 'org.salient.artvideoplayer:artplayer-ui:0.6.0'      //ijkPlayer: optional      implementation 'org.salient.artvideoplayer:artplayer-ijk:0.6.0'      implementation "org.salient.artvideoplayer:artplayer-armv7a:0.6.0"       //Other ABIs: optional      implementation "org.salient.artvideoplayer:artplayer-armv5:0.6.0"      implementation "org.salient.artvideoplayer:artplayer-x86:0.6.0"      // Other ABIs: optional (minSdk version >= 21)      implementation "org.salient.artvideoplayer:artplayer-arm64:0.6.0"      implementation "org.salient.artvideoplayer:artplayer-x86_64:0.6.0"      //ExoPlayer2 : optional      implementation "org.salient.artvideoplayer:artplayer-exo:0.6.0" }Usagejavaimport org.salient.artplayer.VideoView;VideoView videoView = new VideoView(this); videoView.setUp("http://vfx.mtime.cn/Video/2018/06/27/mp4/180627094726195356.mp4"); videoView.setControlPanel(new ControlPanel(this)); videoView.start();xmlAndroidManifest.xml <!-- required -->Activity@Overridepublic void onBackPressed() {  if (MediaPlayerManager.instance().backPress(this)) {      return;   }  super.onBackPressed(); }@Overrideprotected void onPause() {  super.onPause();  MediaPlayerManager.instance().pause(); }@Overrideprotected void onDestroy() {  super.onDestroy();  MediaPlayerMa

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值