gpt4 book ai didi

javascript - getUserMedia API无法从Chrome扩展程序捕获音频和视频

转载 作者:行者123 更新时间:2023-12-02 22:38:07 27 4
gpt4 key购买 nike

我正在使用getUserMedia api捕获屏幕并记录来自chrome扩展程序的音频(两者一起)。 api捕获屏幕,记录视频,但不捕获音频。

Chrome版本:55

没有捕获音频的任何原因。 API是否有任何限制,还是需要使用任何其他API来捕获视频和音频?
请在下面找到代码片段。 (以下代码存在于background.js中)

(尝试将约束传递为音频:true,视频:true,但这没有用。它引发了错误。(错误-getUserMedia()失败:[对象NavigatorUserMediaError]))

chrome.desktopCapture.chooseDesktopMedia(['screen','audio'],
function onAccessApproved(id) {

const constraints = { "video": {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: id,
minWidth: 1280,
minHeight: 720,
maxWidth:1280,
maxHeight:720
}
}, "audio" : {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: id}} };

navigator.mediaDevices.getUserMedia(constraints).then(gotMedia).catch(e => { console.error('getUserMedia() failed: ' + e);
});

function gotMedia(stream) {

theStream = stream;
var video = document.createElement('video');
video.src = URL.createObjectURL(stream);
video.srcObject = stream;

try {
recorder = new MediaRecorder(stream, {mimeType : "video/webm"});
} catch (e) {
console.error('Exception while creating MediaRecorder: ' + e);
return;
}

theRecorder = recorder;
recorder.ondataavailable =
(event) => { recordedChunks.push(event.data); };
recorder.start(100);


stream.getVideoTracks()[0].onended = function () {
download();
};
}


function download() {


theRecorder.stop();
theStream.getTracks().forEach(track => { track.stop(); });

var blob = new Blob(recordedChunks, {type: "video/webm"});
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = 'test.webm';
a.click();
// setTimeout() here is needed for Firefox.
setTimeout(function() { URL.revokeObjectURL(url); }, 100);
}

最佳答案

根据关于屏幕共享的webRTC Experiment's page

Why Screen Sharing Fails?
[...]
2. On chrome, you requested audio-stream alongwith 'chromeMediaSource' – it is not permitted on chrome.



因此,这是不允许的。

您可以尝试仅对音频进行并行请求,然后将两个流与 MediaStream.addTrack() 合并,尽管我没有对其进行测试。
videoStream.addTrack(audioStream.getAudioTracks()[0])

关于javascript - getUserMedia API无法从Chrome扩展程序捕获音频和视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41339406/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com