I’m trying to get Chrome’s getUserMedia to work on my localhost.
My index.php file is:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/script.js"></script>
<title></title>
</head>
<body>
<video id="MediaStreamVideo" autoplay=""></video>
</body>
</html>
My script.js file is:
var stream;
var video = document.getElementById('MediaStreamVideo');
navigator.webkitGetUserMedia(
{video: true, audio: true}, // Options
function(localMediaStream) { // Success
stream = localMediaStream;
video.src = window.webkitURL.createObjectURL(stream);
},
function(err) { // Failure
alert('getUserMedia failed: Code ' + err.code);
}
);
Chrome asks me to allow using the camera and microphone (which I accept), but then nothing happens.
It works fine on jsfidde
Any ideas?
The problem should be with the order in which the script is getting executed. The video element ‘MediaStreamVideo’ will not be available when the script was getting executed.
Its always a good practice to load the scripts at the end. Also, wrap the code inside a function and execute it onload.