原文:https://github.com/electron/electron/blob/master/docs/api/desktop-capturer.md
譯者:Lin
訪問媒體資源的信息,可以被用來使用navigator.webkitGetUserMedia接口從桌面捕捉音頻和視頻。
進程:渲染進程
下面的例子演示如何從桌面上一個標題是Electron的窗口中捕捉視頻:
// 渲染進程中。
const {desktopCapturer} = require('electron')
desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
if (error) throw error
for (let i = 0; i < sources.length; ++i) {
if (sources[i].name === 'Electron') {
navigator.webkitGetUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sources[i].id,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
}, handleStream, handleError)
return
}
}
})
function handleStream (stream) {
document.querySelector('video').src = URL.createObjectURL(stream)
}
function handleError (e) {
console.log(e)
}
使用navigator.webkitGetUserMedia傳入desktopCapturer提供的資源來捕捉視頻,則參數(shù)重必須包含chromeMediaSource: 'desktop',和audio: false。
<h2 id="methods">方法</h2>
desktopCapturer模塊有下面的方法:
<h3 id="desktopCapturer-getSources"><code>desktopCapturer.getSources(options, callback)</code></h3>
-
optionsObject類型-
typesString[]類型 - 一個字符串數(shù)組,列出了要捕捉的桌面資源的類型,可用類型是screen和window。 -
thumbnailSizeObject類型(可選參數(shù))- 建議媒體資源的縮略圖應該縮放到的大小,默認是{width: 150, height: 150}。
-
-
callbackFunction類型-
errorError類型 -
sourcesDesktopCapturerSource[]類型
-
開始收集所有可用的桌面媒體資源,在收集完成后調用callback(error, sources)。
sources是一個DesktopCapturerSource類型對象的數(shù)組,每一個DesktopCapturerSource表示可以被捕捉的一個屏幕或者一個窗口。