我正在使用webrtc作爲第一個教程的web應用程序,我只是試圖通過getUserMedia訪問我的相機和麥克風。如何在Chrome中使用getUserMedia對象播放視頻
這裏是我的代碼:
的index.html:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<nav class="navbar navbar-fixed-top navbar-dark bg-inverse">
<a href="#" class="navbar-brand">Demo WebRTC</a>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-6">
<h2>Réccéption</h2>
<video id="receiver-video" width="100%" height="400px" src=""></video>
</div>
<div class="col-sm-6">
<h2>Envoi</h2>
<video id="emitter-video" width="100%" height="400px" src=""></video>
<p><button id="start">Démarrer la conversation</button></p>
</div>
</div>
</div>
<script src="app.js" charset="utf-8"></script>
</body>
</html>
app.js
document.querySelector('#start').addEventListener('click', function function_name(e) {
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getUserMedia({
video: true,
audio: true
}, function(stream){
let emitterVideo = document.querySelector('#emitter-video')
emitterVideo.src = window.URL.createObjectURL(stream)
emitterVideo.play()
}, function() {})
})
有了這個代碼,我能夠訪問攝像頭和麥克風,並在FireFox中播放視頻,但是當我在Chrome中打開它時,我無法播放視頻,並在控制檯中拋出以下錯誤:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
Promise (async)
(anonymous) @ app.js:15
我使用的是Chrome版本60.0.3112.90。
您使用HTTP還是HTTPS? –