我有这个简单的脚本,我在 webRTC 文档中找到了我尝试运行它,但似乎我错过了一些东西
const leftVideo = document.getElementById('leftVideo');
const rightVideo = document.getElementById('rightVideo');
leftVideo.addEventListener('canplay', () => {
const stream = leftVideo.captureStream();
rightVideo.srcObject = stream;
});
Run Code Online (Sandbox Code Playgroud)
我在检查流捕获时收到此错误 Uncaught DOMException: Failed to execute 'captureStream' on 'HTMLMediaElement': 无法从 HTMLVideoElement.leftVideo.addEventListener 处的跨源数据元素捕获这是我的 index.html
<video id="leftVideo" playsinline controls loop muted>
<source src="test1.webm" type="video/webm"/>
<p>This browser does not support the video element.</p>
</video>
<video id="rightVideo" playsinline autoplay></video>
Run Code Online (Sandbox Code Playgroud)
<video crossOrigin="anonymous" src="https://cdn.myapp.com:81/video.mp4"></video>
Run Code Online (Sandbox Code Playgroud)
您想确保链接使用的是 https
参考:https : //stackoverflow.com/a/35245146/8689969
// Fetch the original image
fetch(video.filePath, {
mode: 'cors',
headers: {
'Access-Control-Allow-Origin':'*'
}
})
// Retrieve its body as ReadableStream
.then(response => {
const reader = response.body.getReader();
return new ReadableStream({
start(controller) {
return pump();
function pump() {
return reader.read().then(({ done, value }) => {
// When no more data needs to be consumed, close the stream
if (done) {
controller.close();
return;
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
return pump();
});
}
}
})
})
.then(stream => new Response(stream))
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob))
.then((url) => {
// gives the blob url which solves cors error in reading stream(using captureStream() func)
console.log(url);
// do your thing
})
.catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3051 次 |
最近记录: |