santosh
santosh

Reputation: 91

How to fix this error : NotReadableError: Could not start video source

I am trying to implement video recording but getting this error. i have 2 buttons called Front Camera ,Back Camera. On click i am calling below method. first it displays the camera correctly but while switching camera it gives me the error. Please help how to fix the problem ?

function StartVideoCamera(obj) {
    var id = $(obj).attr('id');
    var camNode = $(obj).attr('cammode');
    if (camNode != 'user') {
        camNode = eval('{ exact: "environment" }')
    }
    var video = document.querySelector('#video' + id);
    var constraints = { audio: true, video: { width: videoHeight, height: videoWidth, facingMode: camNode 
    } };
    navigator.mediaDevices.getUserMedia(constraints)
    .then(function (mediaStream) {
        window.stream = null;
        video.srcObject = null;
        recordButton.disabled = false;
        window.stream = mediaStream;
        video.srcObject = mediaStream;
        video.onloadedmetadata = function (e) {
            video.play();
        };
    })
    .catch(function (err) {
         alert(err.name + ": " + err.message)
         console.log(err.name + ": " + err.message);
     });
}

Upvotes: 9

Views: 49261

Answers (1)

Shakil Alam
Shakil Alam

Reputation: 445

You need to stop the previous mediaStreamObj before calling getUserMedia again.

This happens when your other(front/back) is already in use. You need to release the camera first.

stream.getTracks()
.forEach(track => track.stop());

Here stream is what you got from getUserMedia

This stops all the devices (you can check that the camera light goes off on the desktop) and (on mobile devices).

Upvotes: 10

Related Questions