我正在尝试使用 Chrome 扩展捕获活动选项卡的音频。我的代码将内容脚本注入给定选项卡,然后使用
chrome.tabs
将流 ID 发送到该内容脚本。
但是,当我尝试开始捕获选项卡的音频时出现错误,即:
Uncaught (in promise) DOMException: Error starting tab capture
。我应该有足够的权限(activeTab
等),所以我不确定是什么导致了这个错误。
我的代码如下:(这是内容脚本)
chrome.runtime.onMessage.addListener((req,sender,res) => {
// Stringify is a function that returns a string given an object, this doesn't cause the error
message = stringify(req.command)
if (message == "captureStream") {
console.log(req.data.streamId)
navigator.mediaDevices.getUserMedia({
video: false,
audio: true,
audio: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: req.data.streamId
}
}
}), (stream) => {
const output = new AudioContext();
const source = output.createMediaStreamSource(stream);
source.connect(output.destination);
}
}
})
你做错了。您不能对字符串使用 stringify。你必须使用 string()