如何在网页上让Microphone静音


普通的audio 增加muted 属性只是让这个audio不发出声音,但是不能控制microphone的录音。
这里要用到2个接口:

navigator.mediaDevices.MediaDevices.getUserMedia()  

返回 MediaStream


通过返回的MediaStream调用

MediaStream.getAudioTracks()  

返回 MediaStreamTrack 对象序列,是对象数组,包含流中所有的音轨。

通过 MediaStreamTrack.muted 这个只读的属性,查看是否 whether or not the track is currently unable to provide media output.


1. 通过stop()方法, Stop the audio stream。 

2. MediaStreamTrack.enabled, 这个enabled属性相当于是mute.  When enabled, a track's data is output from the source to the destination; otherwise, empty frames are output.


const constraints = { audio: true }
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints)

const tracks = mediaStream.getAudioTracks() //这是停掉了这个stream //tracks[0].stop() //如果不想停掉,那就是通过enabled方法,取一个相反的值。 tracks[0].enabled = !tracks[0].enabled

阅读量: 407
发布于:
修改于: