javascript 类型错误:参数 1 对 URL.createObjectURL 的任何 1 参数重载无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52677448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:00:48  来源:igfitidea点击:

TypeError: Argument 1 is not valid for any of the 1-argument overloads of URL.createObjectURL

javascriptgetusermedia

提问by trumanblack1025

I've been using getUserMedia to access camera from the browser. I tried it on several browsers and it works except on firefox. It works on chrome,avast,opera mini. This is my code:

我一直在使用 getUserMedia 从浏览器访问相机。我在几个浏览器上尝试过它,除了在 Firefox 上它都可以工作。它适用于 chrome、avast、opera mini。这是我的代码:

<button type="button" onclick="turnOn()">turn on cam</button>
function turnOn() {
     document.getElementsByTagName('video')[0].play();

     var video = document.querySelector('video')
      , canvas;

    /**
     *  generates a still frame image from the stream in the <video>
     *  appends the image to the <body>
     */

    // use MediaDevices API
    // docs: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
    if (navigator.mediaDevices) {
      // access the web cam
       navigator.mediaDevices.getUserMedia({video: true})
      // permission granted:
        .then(function(stream) {
          video.src = window.URL.createObjectURL(stream);
          /* video.addEventListener('click', takeSnapshot); */
        })
        // permission denied:
        .catch(function(error) {
          document.body.textContent = 'Could not access the camera. Error: ' + error.name + " " + error.message;
        });
    }
}

Hope you could help me. Thank you!

希望你能帮助我。谢谢!

回答by Dietrich Ayala

Replace:

代替:

video.src = window.URL.createObjectURL(stream);

video.src = window.URL.createObjectURL(stream);

with:

和:

video.srcObject = stream

video.srcObject = stream

Source: https://www.chromestatus.com/features/5618491470118912

来源:https: //www.chromestatus.com/features/5618491470118912