getUserMedia - 无法同时从两个浏览器访问摄像头

时间:2017-04-09 13:20:17

标签: javascript webrtc hardware

我一直在尝试使用webRTC来完成我的下一个创建视频聊天和测试项目的难度。我有这个简单的代码来访问相机:

        navigator.getUserMedia = navigator.getUserMedia ||
                                 navigator.webkitGetUserMedia ||
                                 navigator.mozGetUserMedia ||
                                 navigator.msGetUserMedia;

        var video = document.querySelector('#av-chat video');

        if (navigator.getUserMedia) {
          navigator.getUserMedia({audio: true, video: true}, function(stream) {
            video.src = window.URL.createObjectURL(stream);
          }, errorCallback);
        }

在chrome和mozilla上工作得很好但是当我尝试一起做它时,它不起作用。我的意思是在两个浏览器中用这个代码打开相同的文件。似乎当一个浏览器可以访问摄像机时,它会阻止其他任何人使用。

我没有在网上讨论过这个问题,所以我想知道,这只是我吗?如果没有,是否有解决方案?

2 个答案:

答案 0 :(得分:4)

This is a limitation on Windows. On Mac OS you can use the same camera in multiple applications at the same time but not in Windows unfortunately. You have a few options here:

  1. Buy a Mac
  2. Get a second USB camera to use in your second application while testing.
  3. Install some software for splitting your camera or creating a virtual camera.
  4. Use fake devices in either Chrome or Firefox (or both) for testing (like Philipp suggested). It's actually quite easy to turn on fake devices in Firefox and it's free so that's probably the best option if you don't already have another USB camera sitting around.
    1. Just go to about:config in your URL bar
    2. Accept the warning message
    3. Add a new Boolean value. You do this by right clicking and choosing New->Boolean.
    4. Then enter media.navigator.streams.fake
    5. Choose true as the value. enter image description here Now when you use your camera you will get a fake video and audio sound.

答案 1 :(得分:2)

Chrome和Firefox都有命令行标志,可以通过启用假设备来简化测试。有关Chrome的信息,请参阅here for Firefox以及更远一点。