修复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);
	}
}

你可能感兴趣的:(html,js,媒体标签play报错,video的play报错,audio的play报错,html网页play报错,延时修复play报错)