简体   繁体   English

Chrome 扩展程序 - 如何每隔一段时间访问网络摄像头和音频?

[英]Chrome Extension - How to access webcam and Audio at intervals?

I have a chrome extension I am using that I want to integrate with my webcam to do some funky computer vision and audio ML stuff.我有一个正在使用的 chrome 扩展程序,我想将其与我的网络摄像头集成以进行一些时髦的计算机视觉和音频 ML 工作。

usuauly i'd do something liek this,通常我会做一些这样的事情,

for audio,对于音频,

<audio id="player" controls></audio>
<script>
  const player = document.getElementById('player');

  const handleSuccess = function(stream) {
    if (window.URL) {
      player.srcObject = stream;
    } else {
      player.src = stream;
    }
  };

  navigator.mediaDevices.getUserMedia({ audio: true, video: false })
      .then(handleSuccess);
</script>

for webcam对于网络摄像头

<video autoplay></video>

<script>
const constraints = {
  video: true
};

const video = document.querySelector('video');

navigator.mediaDevices.getUserMedia(constraints).
  then((stream) => {video.srcObject = stream});
</script>

With a chrome extension, would the best pathway be to use the background.js file - I am struggling to find any documentation or information about how I could do it with that.使用 chrome 扩展,最好的途径是使用 background.js 文件 - 我正在努力寻找有关如何使用它的任何文档或信息。

Thanks in advance!提前致谢!

You can create an option page (HTML file which lives in extension directory) and have camera/audio permissions granted there once.您可以创建一个选项页面(位于扩展目录中的 HTML 文件)并在那里授予一次相机/音频权限。 after this, you can use these APIs directly in background scripts之后就可以在后台脚本中直接使用这些API了

Here is the example of doing so.这是这样做的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM