Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if selected microphone is muted or not with web audio api

By using the following, we can prompt the user to select their preferred media input device with audio and video source constraints (currently only interested in Chrome support).

navigator.mediaDevices.getUserMedia({audio: true})
  .then((stream) => {
    console.log(stream);
  });

Anyone know if there is an exposed API to detect if the user-selected input device is currently muted or not? The input device would be either an onboard microphone, external mic, or software defined microphone that shows in the system as a hardware device.

like image 425
Geuis Avatar asked Dec 24 '16 02:12

Geuis


1 Answers

You can check property .muted Boolean value of each MediaStreamTrack by iterating the array returned by MediaStream .getAudioTracks() method, or by selecting the MediaStreamTrack by index from the array.

  navigator.mediaDevices.getUserMedia({audio: true})
  .then(stream => {
    console.log("MediaStreamTrack muted:", stream.getAudioTracks()[0].muted);
  })
  .catch(err => console.log(err));

You can also utilize mute and unmute MediaStreamTrack events.

like image 148
guest271314 Avatar answered Oct 16 '22 09:10

guest271314