desktopCapturer
访问有关可用于使用
navigator.mediaDevices.getUserMedia
API 从桌面捕获音频和视频的媒体源的信息。¥Access information about media sources that can be used to capture audio and video from the desktop using the
navigator.mediaDevices.getUserMedia
API.
进程:主进程
¥Process: Main
以下示例显示如何从标题为 Electron
的桌面窗口捕获视频:
¥The following example shows how to capture video from a desktop window whose
title is Electron
:
// main.js
const { app, BrowserWindow, desktopCapturer, session } = require('electron')
app.whenReady().then(() => {
const mainWindow = new BrowserWindow()
session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
// Grant access to the first screen found.
callback({ video: sources[0], audio: 'loopback' })
})
// If true, use the system picker if available.
// Note: this is currently experimental. If the system picker
// is available, it will be used and the media request handler
// will not be invoked.
}, { useSystemPicker: true })
mainWindow.loadFile('index.html')
})
// renderer.js
const startButton = document.getElementById('startButton')
const stopButton = document.getElementById('stopButton')
const video = document.querySelector('video')
startButton.addEventListener('click', () => {
navigator.mediaDevices.getDisplayMedia({
audio: true,
video: {
width: 320,
height: 240,
frameRate: 30
}
}).then(stream => {
video.srcObject = stream
video.onloadedmetadata = (e) => video.play()
}).catch(e => console.log(e))
})
stopButton.addEventListener('click', () => {
video.pause()
})
<!-- index.html -->
<html>
<meta http-equiv="content-security-policy" content="script-src 'self' 'unsafe-inline'" />
<body>
<button id="startButton" class="button">Start</button>
<button id="stopButton" class="button">Stop</button>
<video width="320" height="240" autoplay></video>
<script src="renderer.js"></script>
</body>
</html>
请参阅 navigator.mediaDevices.getDisplayMedia
了解更多信息。
¥See navigator.mediaDevices.getDisplayMedia
for more information.
注意:navigator.mediaDevices.getDisplayMedia
不允许使用 deviceId
选择源 - 见 specification。
¥Note: navigator.mediaDevices.getDisplayMedia
does not permit the use of deviceId
for
selection of a source - see specification.
方法
¥Methods
desktopCapturer
模块有以下方法:
¥The desktopCapturer
module has the following methods:
desktopCapturer.getSources(options)
返回 Promise<DesktopCapturerSource[]>
- 使用 DesktopCapturerSource
对象数组进行解析,每个 DesktopCapturerSource
代表一个屏幕或可以捕获的单个窗口。
¥Returns Promise<DesktopCapturerSource[]>
- Resolves with an array of DesktopCapturerSource
objects, each DesktopCapturerSource
represents a screen or an individual window that can be captured.
注意 在 macOS 10.15 Catalina 或更高版本上捕获屏幕内容需要用户同意,这可以被 systemPreferences.getMediaAccessStatus
检测到。
¥Note Capturing the screen contents requires user consent on macOS 10.15 Catalina or higher,
which can detected by systemPreferences.getMediaAccessStatus
.
注意事项
¥Caveats
navigator.mediaDevices.getUserMedia
无法在 macOS 上进行音频捕获,因为存在一个基本限制,即想要访问系统音频的应用需要 签名的内核扩展。Chromium 以及 Electron 并没有提供这一点。
¥navigator.mediaDevices.getUserMedia
does not work on macOS for audio capture due to a fundamental limitation whereby apps that want to access the system's audio require a signed kernel extension. Chromium, and by extension Electron, does not provide this.
可以通过使用 Soundflower 等其他 macOS 应用捕获系统音频并将其通过虚拟音频输入设备传递来规避此限制。然后可以使用 navigator.mediaDevices.getUserMedia
查询该虚拟设备。
¥It is possible to circumvent this limitation by capturing system audio with another macOS app like Soundflower and passing it through a virtual audio input device. This virtual device can then be queried with navigator.mediaDevices.getUserMedia
.