如何使用javascript使相机工作

如何使用javascript使相机工作,javascript,html,camera,video-streaming,getusermedia,Javascript,Html,Camera,Video Streaming,Getusermedia,我知道这个问题已经被问了很多次了,但我无法找出我的代码中的错误。我在谷歌、这里以及其他许多我还没有找到解决方案的地方寻找过。 我的HTML代码是: <button id="camera">Show the camera</button> <video id="video" class="hidden" preload="none" autoplay> <source src="video.webM" type="video/webM"> &

我知道这个问题已经被问了很多次了,但我无法找出我的代码中的错误。我在谷歌、这里以及其他许多我还没有找到解决方案的地方寻找过。 我的HTML代码是:

<button id="camera">Show the camera</button>
<video id="video" class="hidden" preload="none" autoplay>
    <source src="video.webM" type="video/webM">
</video>  
在手机中,我得到了浏览器请求我访问相机的权限,但一旦我接受,什么都不会发生:没有错误,没有视频,没有流化,什么都没有。 另外,我忘了提到,但是web是在SSL服务器中的,所以没有这个问题。 谁能给我举个例子吗??。我不知道还能做什么:(
谢谢

视频流的设置不正确。在成功回调中,您需要

video.srcObject = localMediaStream;
您将新的promise API与旧的不推荐使用的回调API混淆了

这里还有很多过时的供应商前缀cruft(例如,
video.mozSrcObject
不再存在)

只是:

如果您担心支持旧浏览器,请使用。

这应该可以

 navigator.mediaDevices.getUserMedia({ video: true, audio: false })
    .then(localMediaStream => {
        console.log(localMediaStream);
        video.src =window.URL.createObjectURL(localMediaStream);
    }).catch(err =>{
        console.error('oh why errors!!', err);
    });

抱歉,但是当我使用你的代码时,我得到了一个语法错误,但这很奇怪,因为在你发布的fiddle示例中,它看起来工作得很好。你知道吗???@galeonweb我怎么知道?错误和行数是什么?浏览器和平台是什么?尝试从fiddle中删除,看看这是否有区别。但是我应该把línea放在哪里?我有你的remov吗e我的??或者只是添加它?,对不起,我的ignoranceAs@jib提到了,您的一些方法调用已经过时。在
console.log(“摄像头已激活”);
之后,成功回调中的所有内容都可以替换为上面的行。
navigator.mediaDevices.getUserMedia({video: true})
  .then(stream => video.srcObject = stream)
  .catch(e => console.error(e));
 navigator.mediaDevices.getUserMedia({ video: true, audio: false })
    .then(localMediaStream => {
        console.log(localMediaStream);
        video.src =window.URL.createObjectURL(localMediaStream);
    }).catch(err =>{
        console.error('oh why errors!!', err);
    });