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

时光总嘲笑我的痴心妄想 提交于 2020-05-27 04:02:34

问题


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.


回答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.



来源:https://stackoverflow.com/questions/41309682/check-if-selected-microphone-is-muted-or-not-with-web-audio-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!