【问题标题】:TypeError: Argument 1 is not valid for any of the 1-argument overloads of URL.createObjectURLTypeError:参数 1 对 URL.createObjectURL 的任何 1 参数重载均无效
【发布时间】:2018-10-06 09:08:42
【问题描述】:

我一直在使用 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;
        });
    }
}

希望你能帮助我。谢谢!

【问题讨论】:

    标签: javascript getusermedia


    【解决方案1】:

    替换:

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

    与:

    video.srcObject = stream

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

    【讨论】:

    • 如果我将URL.createObjectURL(stream); 放入&lt;img src={URL.createObjectURL(stream) /&gt; 会怎样?如何更改为 video.srcObject = stream?
    • 嗨@jhon - 你能提出一个新的问题吗?这是一个与原始问题非常不同的上下文,并且需要一个完整的示例才能正确回答。
    • 对此很抱歉,但它已经解决了。谢谢你的回复
    猜你喜欢
    • 2022-08-05
    • 2019-09-30
    • 2013-04-19
    • 2016-07-29
    • 1970-01-01
    • 2017-03-22
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多