电子应用中使用Angular Spa的桌面录制

发布于 2025-02-09 07:10:28 字数 2193 浏览 3 评论 0原文

我大家。我在包含Angular的水疗中心的电子中应用。电子通过使用IPCrender和包含从ipcmain.on派生的事件的JavaScript文件来揭示其API的服务,以便它可以在电子和Angular之间进行通信。 Angular调度发送事件:

// The event in the Javascript file

ipcMain.on('desktop-capture', async () => {
    const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
    console.log(sources); // qui è dove si interrompe l’esecuzione
    for (const source of sources) {
      if (source.name === ‘Entire ’Screen) mainWindow.webContents.send('desktop-capture-res', [source]);
    }
  });

在TS文件中,我通过发送“桌面捕获”事件来初始化桌面捕获对电子API的响应。 (下图)我使用电子API的源要求用户允许记录屏幕:

initializeDesktop() {
this.ipc.send('desktop-capture');
this.ipc?.on('desktop-capture-res', (event, source: Electron.DesktopCapturerSource) => {
  navigator.mediaDevices.getUserMedia({}).then((stream: MediaStream) => this.getDesktop(stream));
  console.log('DESKTOP_CAPTURE_STREAM', source);
})
};

我使用电子API的源来要求用户允许记录屏幕:

getDesktop(stream: MediaStream) {
setInterval(() => {
  const mediaRecorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
  mediaRecorder.start();
  const chunks: any[] = [];
  mediaRecorder.ondataavailable = (e) => {
    chunks.push(e.data);
  };
  setTimeout(() => {
    mediaRecorder.stop();
  }, API_SEND_TIMEOUT);
  mediaRecorder.onstop = (e) => {
    const blob = new Blob(chunks.splice(0, chunks.length));
    console.log('DESKTOP_CAPTURE_BLOB', blob);
    this.videos.push(blob);
  };
}, API_SEND_TIMEOUT)};

不幸的是,这是行不通的。 electron文档建议使用此说法来指定源桌面,但是TS不认识视频的强制性属性。

try {
      mediaDevices.getUserMedia({
          audio: false,
          video: {
              mandatory: {
                  chromeMediaSource: 'desktop',
                  chromeMediaSourceId: source.id,
                  minWidth: 1280,
                  maxWidth: 1280,
                  minHeight: 720,
                  maxHeight: 720
              }
          }
      }).then((stream) => {
      });
  }

感谢您的帮助。

I everyone. I have an application in Electron containing a SPA in Angular. Electron is used through a service that exposes its API using ipcRenderer and a javascript file that contains events derived from ipcMain.on, so that it can communicate between Electron and Angular. Angular dispatches a SEND event:

// The event in the Javascript file

ipcMain.on('desktop-capture', async () => {
    const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
    console.log(sources); // qui è dove si interrompe l’esecuzione
    for (const source of sources) {
      if (source.name === ‘Entire ’Screen) mainWindow.webContents.send('desktop-capture-res', [source]);
    }
  });

In the TS file, I initialize the desktop capture by sending the 'desktop-capture' event that takes care of getting the resource to be used (above) and sending a 'desktop-capture-res' event containing the response to the electron API.
(Below) I use the source from the Electron API to ask the user for permission to record the screen:

initializeDesktop() {
this.ipc.send('desktop-capture');
this.ipc?.on('desktop-capture-res', (event, source: Electron.DesktopCapturerSource) => {
  navigator.mediaDevices.getUserMedia({}).then((stream: MediaStream) => this.getDesktop(stream));
  console.log('DESKTOP_CAPTURE_STREAM', source);
})
};

I use the source from the Electron API to ask the user for permission to record the screen:

getDesktop(stream: MediaStream) {
setInterval(() => {
  const mediaRecorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
  mediaRecorder.start();
  const chunks: any[] = [];
  mediaRecorder.ondataavailable = (e) => {
    chunks.push(e.data);
  };
  setTimeout(() => {
    mediaRecorder.stop();
  }, API_SEND_TIMEOUT);
  mediaRecorder.onstop = (e) => {
    const blob = new Blob(chunks.splice(0, chunks.length));
    console.log('DESKTOP_CAPTURE_BLOB', blob);
    this.videos.push(blob);
  };
}, API_SEND_TIMEOUT)};

This unfortunately does not work. The Electron documentation suggests using this statement to specify the source desktop, but TS does not recognize the Mandatory property of video.

try {
      mediaDevices.getUserMedia({
          audio: false,
          video: {
              mandatory: {
                  chromeMediaSource: 'desktop',
                  chromeMediaSourceId: source.id,
                  minWidth: 1280,
                  maxWidth: 1280,
                  minHeight: 720,
                  maxHeight: 720
              }
          }
      }).then((stream) => {
      });
  }

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文