logo
On this page

Volume Change


Overview

Volume change: refers to the volume level of a stream, referred to hereinafter as "sound level".

The main application scenario is to determine which user on the mic is speaking during the publish/play process, and display it in the UI, for example:

Prerequisites

Before monitoring sound levels, ensure:

Usage Steps for Non-stream-mixing Scenarios

Sound Level Callback Interfaces

  • Interface prototype:

    Local captured sound level callback interface capturedSoundLevelUpdate:

    /**
     * Local captured audio sound level callback
     *
     * @param soundLevel Local captured sound level value, ranging from 0.0 to 100.0
     */
    capturedSoundLevelUpdate: (soundLevel: number) => void;

    Remote audio sound level callback interface remoteSoundLevelUpdate:

    /**
     * Remote audio sound level callback
     *
     * @param soundLevels Remote sound level key-value pairs, key is the stream ID, value is the sound level value of the corresponding stream, value ranges from 0.0 to 100.0
     */
    remoteSoundLevelUpdate: (soundLevels: {[key: string]: number}) => void;
  • Usage example:

    The callback for remote playing stream sound levels returns a map, where "key" is the stream ID of other users currently publishing streams in the room, and "value" is the sound level data corresponding to that stream.

    You can first obtain and save the list of existing streams in the current room through the roomStreamUpdate callback method, and then use the saved stream list to index the map to obtain the sound level data corresponding to each stream.

    The following example shows how to obtain sound level data from the callback method:

    // Listen to remote sound level callback notification
    ZegoExpressEngine.instance().on('remoteSoundLevelUpdate', (soundLevels) => {
        // Index remote sound level value in soundLevels map through streamID
        });
    
    // Listen to local sound level callback notification
    ZegoExpressEngine.instance().on('capturedSoundLevelUpdate', (soundLevel) => {
        // Get local sound level value directly
        });

Start Monitoring Sound Level Callback

Call the startSoundLevelMonitor interface to start monitoring sound level callbacks.

// Start sound level monitoring
ZegoExpressEngine.instance().startSoundLevelMonitor;

After calling the above interface, the capturedSoundLevelUpdate callback method will only have a callback after calling the startPreview interface to start preview or the startPublishingStream: interface to start publishing stream.

remoteSoundLevelUpdate will only have a callback after calling the startPlayingStream interface to start playing stream.

Stop Monitoring Sound Level Callback

Call the stopSoundLevelMonitor interface to stop monitoring sound level callbacks.

// Stop sound level monitoring
ZegoExpressEngine.instance().stopSoundLevelMonitor();

After calling the above interface, capturedSoundLevelUpdate and remoteSoundLevelUpdate will no longer have callbacks.

FAQ

Why didn't I receive the relevant callback after turning on the sound level monitoring switch?

  • The local captured sound level callback will be triggered immediately, and the callback value is 0 when not publishing stream.
  • The remote playing stream sound level callback will only be triggered after successfully calling the startPlayingStream interface to play stream.

Previous

Real-time Messaging and Signaling

Next

Ear Monitor and Channel Settings

On this page

Back to top