WebRTC: Get audio level of a mediaStream *without* playing back the audio

audio, javascript, webrtc

Solution

Take a look at `createGain` method. It allows you to set stream's volume.

Here is my (simplified) example that I use in my project:

navigator.getUserMedia({audio: true, video: true}, function(stream) {
    var audioContext = new AudioContext; //or webkitAudioContext
    var source = audioContext.createMediaStreamSource(stream);

    var volume = audioContext.createGain();
    source.connect(volume);
    volume.connect(audioContext.destination);
    volume.gain.value = 0;  //turn off the speakers

    //further manipulations with source
}, function(err) {
    console.log('error', err);
});

Problem

I'm looking to get the microphone activity level of a WebRTC MediaStream. However, I need to get this information without playing back the microphone to the user (otherwise there will be the loopback effect). The answer in Microphone activity level of WebRTC MediaStream relies on the audio being played back to the user. How can I do this, without playing back the microphone?

Original source