uniapp-微信小程序/抖音小程序video组件使用回调timeupdate后,部分性能不佳真机上发生黑屏或卡顿分析解决【video】

最近刚好做个小程序视频播放项目,记得上一次使用video还是微信小程序刚出那会,性能啊层级问题一堆,时隔多年,以为这玩意随便用了吧,万万没想到做了个自定义控制键就翻车了,h5或者开发者工具上顺畅的一批,自己的手机没问题,一到客户手机就裂开了。提了小程序技术工单,迟迟不能解决我的问题。

关于这个问题僵持了好几天,无奈,只能委托客户帮忙联调,经过层层代码的注释,发现timeupdate回调方法里 第一次赋值就已经出问题了,只是存了currentTime.......

观察了下timeupdate 在1s内会触发多次,感觉就是刷的太频繁了,ui更新导致性能差设备上播放器黑屏了,针对timeupdate进行异步+节流优化。

/**
 * @播放进度变化时触发
 */
async timeupdate(e) {
	await this.timeupdateAsync(e);
},
async timeupdateAsync(e) {//采用异步进程防止堵塞主进程
	const _this= this
	const duration = Number((e.detail.duration || 0).toFixed(2))//存储播放时长,用于前进后退
	const now = new Date().getTime();
	const currentTimeInteger = Number((e.detail.currentTime || 0).toFixed(0))//
	// 模拟异步存储操作
	return new Promise((resolve) => {

		setTimeout(() => {
			if (now - this.lastUpdateTime >= 1000) {//1s一次 如果卡可以设置大一点如2000  3000 
				this.lastUpdateTime = now;
				if(!_this.srcs && _this.duration !== duration){//避免重复赋值
					_this.duration = Number((( duration) || 0).toFixed(2))
				}
				
			}

			this.currentTime = currentTimeInteger//导致黑屏卡顿的主因
			resolve();
		}, 0);
	});
},

 lastUpdateTime定义在data里,视频源更换需重置为0

黑屏卡顿问题完美解决

理论上说限制timeupdate就可以解决这个问题,文档上video没有限制timeupdate频率的配置.....

posted @ 2024-07-23 11:08  昌子玩前端  阅读(116)  评论(0)    收藏  举报