给定代码
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<video width="320" height="280"></video>
<script>
const video = document.querySelector("video");
const src = "https://mirrors.creativecommons.org/movingimages/webm/ScienceCommonsJesseDylan_240p.webm#t=0,10";
let recorder;
video.oncanplay = () => {
video.play();
const mediaStream = video.captureStream();
recorder = new MediaRecorder(mediaStream);
recorder.ondataavailable = event =>
console.log(URL.createObjectURL(event.data));
recorder.start();
}
video.onpause = () => recorder && recorder.stop();
video.src = src;
</script>
</body>
</html>
Chromium 浏览器使用 捕获<video>
元素的回放.captureStream()
, 以及实例事件的结果日志MediaRecorder()
。Blob URL
Blob
dataavailable
MediaRecorder
在当前实现.mozCaptureStream()
错误的 Firefox 浏览器中,会抛出recorder.start()
callSecurityError: The operation is insecure.
和 at recorder.stop()
call InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
。
这些错误显然是由于来自 DOM Elements的媒体捕获的安全考虑。
我们如何才能避免上述错误并达到 Chromium 和 Chrome 浏览器在 Firefox 浏览器上实现的相同结果?