Geuis
Geuis

Reputation: 42297

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.

Upvotes: 8

Views: 5432

Answers (1)

guest271314
guest271314

Reputation: 1

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.

Upvotes: 9

Related Questions