如何使用webRTC启动videostream?

我正在编写下面的代码来启动我的相机,并使用技术webRTC在Google Chrome浏览器上观看我的video。 有两个文件我创build了index.html和client.js。 我附上了两者的代码。 Node.js服务器安装在我的电脑上。 问题是我的相机正在打开,但我无法看到videostream。

client.js

function hasUserMedia() { //check if the browser supports the WebRTC return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia); } if (hasUserMedia()) { navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; //enabling video and audio channels navigator.getUserMedia({ video: true, audio: true }, function (stream) { var video = document.querySelector('video'); //inserting our stream to the video tag video.src = window.URL.createObjectURL(stream); }, function (err) {}); } else { alert("WebRTC is not supported"); }` 

的index.html

 <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8" /> <link rel="stylesheet" href="css/main.css" /> </head> <body> <video autoplay></video> <script src = "js/client.js"></script> </body> </html> 

看来你使用的是旧代码,现在API已经变得更好了,开始使用最新的API。

查看演示和来源

在你的client.js中尝试下面的代码片段

 var constraints = { audio: true, video: true}; navigator.mediaDevices.getUserMedia(constraints).then(function(stream) { var video = document.querySelector('video'); video.srcObject = stream; }).catch(function(err) { console.log('Error in getting stream', err); });