Trying to ask for users permission to use camera and microphone in a React app

I am using navigator.mediaDevices.getUserMedia({ audio: true, video: true }) to ask for permission to use my users' mic and webcam in advance and it works, however, it causes the webcam to light up (which might look like the app is recording or using the camera when it's not). Unless by asking permission I am forcing the camera to be used which I am not sure about. My questions are:

  1. Is using the navigator the correct way to request permission? And if so
  2. How do I just ask for permission and not "turn on" or light up the users' camera too early?

Upvotes: 6

Views: 10026

Answers (1)

user120242
user120242

Reputation: 15268

Demo here: https://js.do/code/so62273679

Take the stream, get the tracks and stop them to turn off the camera. Camera will still flash, but it will turn off.

navigator.getUserMedia({audio:true,video:true}, function(stream) {
  stream.getTracks().forEach(x=>x.stop());
}, err=>console.log(err));

Upvotes: 5

Related Questions