In my creation of account I have a camera button that when click it will show a modal inside of it has a video stream and have a button to take a photo, Everything is working I'm just struggling on how I can stop the webcam stream when the user click the x button on modal but when I click the x button it's still on. Hope you can help fix it , Advance Thanks.
Here is the code for the webcam:-
<a class="btn-floatingbtn-lg" data-toggle="modal" data-target="#webcam" onClick="startWebcam();"><i class="fa fa-camera fa-2x"></i></a>
<div class="modal fade" id="webcam" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="false">
<div class="modal-dialog cascading-modal" role="document">
<div class="modal-content">
<div class="modal-header light-blue darken-3 white-text ">
<h4 class="title "><i class="fa fa-camera"></i> WebCam</h4>
<a type="btn" onclick="vidOff()" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></a>
</div>
<div class="modal-body mb-1">
<div class="booth">
<video id="video" width="270" height="200" ></video>
<a href="#" id="capture" class="btn btn-primary" style="display:block; margin: 10px 0;padding:10px 20px; text-align:center;text-decoration:none;">
Take photo</a>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my javaScript I used for webcam:-
function startWebcam(){
var video = document.getElementById('video'),
canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
photo = document.getElementById('photo'),
vendorUrl = window.URL || window.webkitURL;
navigator.getMedia = navigator.getUserMedia||
navigator.webkitGetUserMedia||
navigator.mozGetUserMedia||
navigator.msGetUserMedia;
navigator.getMedia({
video: true,
audio: false
}, function(stream){
video.srcObject = stream;
video.play();
}, function(error){
// An error occured
// error.code
});
document.getElementById('capture').addEventListener('click',function(){
context.drawImage(video, 0, 0, 270, 200);
photo.setAttribute('src', canvas.toDataURL('image/png'));
});
};
function vidOff() {
video.pause();
video.srcObject = "";
};
When I run my code on console I got this error:
Uncaught TypeError: Failed to set the 'srcObject' property on 'HTMLMediaElement': The provided value is not of type 'MediaStream'.