我一直在寻找,但没有找到解决这个问题的方法。
我不确定为什么,但我得到的stream
是null
。
正如您在下面的代码中所看到的,我没有使用browserAction
,而是在向扩展程序发送消息然后调用requestCurrentTabCapture
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
switch(msg.type){
case 'SS_TAB_REQUEST':
alert("SS_TAB_REQUEST");
requestCurrentTabCapture(port, msg);
break;
case 'SS_TAB_CANCEL':
break;
default:
}
});
});
function requestCurrentTabCapture(port, msg){
//chrome.tabs.getCurrent(function(tab) {
//chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.query({active: true}, function(tab) {
alert(tab);
chrome.tabCapture.capture(
{
video: true, audio: true,
videoConstraints: {
mandatory: {
chromeMediaSource: 'tab',
minWidth: 16,
minHeight: 9,
maxWidth: 1280,
maxHeight: 720,
maxFrameRate: 60, // Note: Frame rate is variable (0 <= x <= 60).
},
},
},
function(stream) {
alert(stream); //<-- NULL
if (!stream) {
console.error('Error starting tab capture: ' + chrome.runtime.lastError.message || 'UNKNOWN'));
}
}
);
});
chrome.tabCapture.onStatusChanged.addListener(function (info){
alert("status: "+info.status); //<-- not shown
alert("tabId: "+info.tabId); //<-- not shown
alert("fullscreen: "+info.fullscreen); //<-- not shown
});
}
权限如下:
"permissions": [
"desktopCapture",
"<all_urls>",
"tabs",
"activeTab",
"tabCapture"
]