javascript - 使用 Electron 记录桌面

标签 javascript typescript electron

我正在构建一个需要 desktopCapturer 的 Electron 。 api,但我不完全了解如何使用它。

从 api 官方页面(和这个示例应用程序: https://github.com/hokein/electron-sample-apps/tree/master/desktop-capture )我看到 desktopCapturer只给我来源的 id,而不是视频流本身。为此,我应该使用 navigator.mediaDevices.getUserMedia() .但是约束对象不再具有 mandatory属性,并且因为我使用的是 typescript ,所以如果我尝试使用它,我会收到错误消息。

我尝试使用 deviceId属性,但我收到此错误:Uncaught (in promise) DOMException: Requested device not found (在带有网络摄像头的设备上,我会得到网络摄像头流而不是那个错误)。这是我的代码:

import { desktopCapturer, DesktopCapturerSource } from "electron";

function onLoad(){
    desktopCapturer.getSources({
        thumbnailSize: {
            width: 256,
            height: 256,
        },
        types: ["screen", "window"]
    }, (error: Error, srcs: DesktopCapturerSource[]) => {
        if (error)
            throw error;
        let video: HTMLVideoElement | null = document.querySelector("video");
        for (let src of srcs)
            navigator.mediaDevices.getUserMedia({
                video:{
                    deviceId : src.id
                }
            }).then((stream:MediaStream)=>{
                if(video){
                    video.srcObject = stream;
                    video.play();
                }
            })
    })
}

document.addEventListener("DOMContentLoaded", onLoad);

我也尝试使用 navigator.getDisplayMedia() ,但我不会像在 Chrome 中那样弹出选择源的提示。我应该怎么做才能让它工作?提前致谢!

最佳答案

我找到了解决方案,至少对于新的解决方案,因为 WebRTC 尚未标准化。将导航器对象复制到变量中并强制转换为 any允许使用 mandatory约束对象上的属性,因为 typescript 不再检查类型兼容性

关于javascript - 使用 Electron 记录桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562401/

相关文章:

javascript - 在JS中,这个特性叫什么?

javascript - 在工具提示中显示表格单元格内容(jsfiddle 在实际站点上不起作用)

javascript - 无法使用super调用继承的类函数

带有子项的 ReactJS TypeScript 纯组件

angular - Typescript + Angular 2 : How can I ensure emitted decorator metadata will refer to variables in scope, 以避免 "not defined"错误?

electron - ejs-electron 不能与 Electron 锻造一起使用

javascript - 如何捕获指令中的复选框事件?

javascript - 在angularjs中使用restful api发出get请求

javascript - 无法在文本框中添加小数

javascript - 通过nodejs运行可执行文件