js video清空src

76 阅读1分钟
let video = documnet.querySelector('video')
try {
	if ('srcObject' in video) {
	  	video.srcObject = null;
	} else {
	  // Avoid using this in new browsers, as it is going away.
	  	video.src = null;
	}
} catch(e){
	console.log(e)
}
注意
  • src置为''无法清空video 且报错Uncaught TypeError: Failed to set the 'srcObject' property on 'HTMLMediaElement': Failed to convert value to 'MediaStream'.
  • src置为null可清空video 但报错Cannot set properties of null (setting 'srcObject')会阻塞后续执行 - 解决办法:用 try catch 包裹