问题
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