我正在编写以下代码,以使用webRTC技术在Google Chrome浏览器上启动我的摄像头并观看我的视频。我创建了两个文件index.html和client.js。我已经附上了两者的代码。Node.js服务器已安装在我的PC上。问题是我的相机正在开启,但我无法看到视频流。
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>