首页 > 解决方案 > Electron desktopCapturer.getSources().then 不是函数

问题描述

在 Windows 10 Pro 版本 1809 上开发电子应用程序。我们的应用程序有一个浏览器窗口,当用户点击热键组合时会呈现。浏览器窗口调用“renderer.js”

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Audio Visualizer</title>
  </head>
  <body>

    <!-- Content -->

    <script>
      require('./renderer.js')
    </script>
  </body>
</html>

renderer.js文件处理来自该浏览器窗口中的用户的交互,并尝试desktopCapturer使用屏幕(整个屏幕)作为源来捕获用户的音频。

我按照电子文档上的文档进行操作,但出现错误desktopCapturer.getSources().then is not a function

这是renderer.js文件

desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
  for (const source of sources) {
    if (source.name === 'Electron') {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({
          audio: {
            mandatory: {
             chromeMediaSource: 'desktop',
             chromeMediaSourceId: source.id 
            }
          },
          video: false
        })
        handleStream(stream)
      } catch (e) {
        handleError(e)
      }
      return
    }
  }
})

function handleStream (stream) {
  // create audio context and show audio visulaizaio
}

function handleError (e) {
  console.log(e)
}

我们的代码基本上是电子文档中包含的示例的直接复制/粘贴。我们错过了什么,我们得到了这个错误?

我知道 macOS 存在一个限制,您必须以不同的内容(而不是 navigator.mediaDevices.getUserMedia)创建流,但我认为该限制与我们面临的这个错误无关。

标签: javascriptnode.jswindowselectrongetusermedia

解决方案


推荐阅读