【发布时间】:2020-03-20 14:53:53
【问题描述】:
我正在尝试开发一个 Chrome 扩展程序来访问网络摄像头并录制视频,并在停止后下载它。
在调用以下代码行时扩展失败并出现错误:
navigator.mediaDevices.getUserMedia({ audio: false, video: true }).then(handleSuccess).catch(function (err) {alert(err)});".
错误是:
NotAllowedError: Failed due to shutdown
我在 Mac 上使用 Chrome 版本 80.0.3987.132(官方构建)(64 位)。
我在这里做错了什么?我们不能通过扩展访问设备网络摄像头吗?有什么指点吗?
示例代码如下:
manifest.json
{
"manifest_version": 2,
"name": "Video Capture",
"version": "0.1",
"browser_action": {
"default_icon": "logo.png",
"default_popup": "popup.html"
}
}
popup.html
<html>
<head>
<title>Video Capture</title>
<script src="videoCapture.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<a id="download">Download</a>
</body>
</html>
videoCapture.js
var shouldStop = false;
window.addEventListener('load', function showPopup() {
alert("Extension started");
startButton = document.getElementById('start');
stopButton = document.getElementById('stop');
downloadLink = document.getElementById('download');
shouldStop = false;
startButton.addEventListener('click', function() {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
alert("This browser does not support the API yet");
}
alert("Media Devices Available..Starts recording.");
var handleSuccess = function(stream) {
alert("handling recording");
const options = {mimeType: 'video/mp4'};
const recordedChunks = [];
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start(5000); //capture video for 5 seconds
mediaRecorder.ondataavailable = function(e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
if(shouldStop === true && stopped === false) {
mediaRecorder.stop();
stopped = true;
stream.getTracks().forEach(function(track) {
track.stop();
});
}
};
mediaRecorder.onstop = function() {
downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
var currentTimestamp = Date.now();
downloadLink.download = 'recording-'+currentTimestamp+'.mp4';
alert("Click Download Link to download captured video");
};
};
navigator.mediaDevices.getUserMedia({ audio: false, video: true })
.then(handleSuccess).catch(function (err) {alert(err)});
});
stopButton.addEventListener('click', function() {
shouldStop = true;
alert("stopped");
});
});
【问题讨论】:
-
听起来像一个错误,请参阅crbug.com。
-
我在错误中找不到类似的问题。我自己刚刚养了一个。
-
@Anoop - 你解决了这个问题吗?
-
@Heiki,该错误已关闭为“不会修复”。所以,还没有运气。
标签: javascript google-chrome-extension getusermedia