Tutorial: Detecting Volume

Detecting Volume

This document describes how to detect volume. The two main purposes of detecting volume are:

  • To check whether the mic functions properly
  • To get the volume of users during an audio/video call

Method

You can call getAudioLevel() to get the current volume.

const volume = stream.getAudioLevel();
if (volume > 0.1) {
console.log(`${stream.getUserId()} is speaking`);
}

Best Practices

If you need to listen to the volume level throughout the video/audio call, you can add event listener to 'audio-volume' event,
and you can call enableAudioVolumeEvaluation() method.

client.on('audio-volume', event => {
  event.result.forEach(({ userId, audioVolume, stream }) => {
    console.log(`userId: ${userId}, audioVolume: ${audioVolume}`);
  })
})
// Turn on volume callbacks and set events to fire every 1000ms
client.enableAudioVolumeEvaluation(1000);
// To turn off the volume callback, pass in an interval value less than or equal to 0
client.enableAudioVolumeEvaluation(-1);