Configure volume detection

Updated at:

ApsaraVideo Real-time Communication (ARTC) SDK for Web allows you to detect the input volume of the local microphone and the volume of remote users.

Implementation

To detect volume levels for the local microphone and remote users, listen to the audioVolume event and call the enableAudioVolumeIndication method.

// Make sure that an AliRtcEngine instance is created.
// Listen to the event.
aliRtcEngine.on('audioVolume', (speakers) => {
  // The array of user volume objects returned by the event.
  speakers.forEach(({ userId, volume }) => {
    // The volume is a numeric value. Valid values: 0 to 100.
    if (userId === '') {
      // If the userId parameter is left empty, the volume is the input volume of the local microphone.
      console.log(`my volume: ${volume}`);
    } else {
      // The volume of the remote user.
      console.log(`user(${userId}) volume: ${volume}`);
    }
  });
});

// Enable volume detection and specify that the audioVolume event is triggered every 1,000 milliseconds.
aliRtcEngine.enableAudioVolumeIndication(1000);

// Disable volume detection by specifying a value less than or equal to 0.
aliRtcEngine.enableAudioVolumeIndication(0);