JS how can I separately check access to webcam and microphone in Firefox

Viewed 843

I need to know if there is a way to check access to a microphone or to a webcam for Firefox not using navigator.mediaDevices.getUserMedia . For example, this code navigator.permissions.query({name : 'camera'}) works very well for Chrome, but Firefox doesn't support camera and microphone in permission.query. The reason why I do not want to use navigator.mediaDevices.getUserMedia is that the permission modal for the camera and microphone will be shown separately if I make it like this navigator.mediaDevices.getUserMedia({video: true}) for webcam and navigator.mediaDevices.getUserMedia({audio: true}) for microphone. Thanks.

1 Answers

For anyone looking for an answer, this isn't perfect but it does separate the permissions. If you want the audio permission to still fire when you deny permission to video, then take the getUserMedia function out of the promise chain.

Also, this is in coffeescript.

cameraAttributes = {}
navigator.mediaDevices.getUserMedia {video: true}
.then ->
  navigator.mediaDevices.enumerateDevices()
.then (devices) =>
  videoDevices = devices.filter (test) -> test.kind is 'videoinput'
  cameraAttributes = 
    videoDevices: videoDevices
  cameraAttributes
.then ->
  navigator.mediaDevices.getUserMedia {audio: true}
  .then (devices) =>
    audioDevices = devices.filter (test) -> test.kind is 'audioinput'
    Object.assign cameraAttributes, audioDevices
    cameraAttributes
  .catch (error) =>
    if error instanceof DOMException
      console.log "Audio - #{error}"
    else
      console.error error
    cameraAttributes
.catch (error) =>
  if error instanceof DOMException
    console.log "Video - #{error}"
  else
    console.error error
  cameraAttributes = 
    videoDevices: []
    audioDevices: []
  cameraAttributes
Related