修复The play() request was interrupted等系列问题

1. 问题

在网页端播放媒体,如video、audio等标签,在频繁切换组件播放时经常出现The play() request was interrupted 等错误提示,
比如Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()或者Uncaught (in promise) DOMException: The play() request was interrupted by a new load request

2. 原因

不管是移除还是暂停等等错误,都是组件切换的时候play是异步的过程,所以需要延时执行操作。

3. 修复方式

this.timer = setTimeout(() => {
    
    
    this.video && this.video.play();
},500);

经过多次试验,定时操作设置500毫秒一般不再报错。

当然,别忘了组件销毁时需要移除定时器,比如

export default {
    
    
	beforeDestroy() {
    
    
		clearTimeout(this.timer);
	}
}

猜你喜欢

转载自blog.csdn.net/wzp20092009/article/details/133668087