logo
On this page

Audio Spectrum and Volume Change


Introduction

  • Volume Change: Refers to the volume level of a specific stream, referred to below as "sound level".

  • Main application scenarios: During the publishing and playing process, determine which user on the microphone is speaking and display it on the UI, for example:

  • Audio Frequency Spectrum: The energy value of digital audio signals at various frequency points.

  • Main application scenarios: In host KTV scenarios, on the premise of having published or played streams, let the host or audience see animations of pitch changes and volume changes, for example:

Example Source Code Download

Please refer to Download Example Source Code.

For related source code, please check files in the /ZegoExpressExample/src/SoundLevel directory.

Usage Steps

Enable/Disable Sound Level and Spectrum Monitoring

The SDK has the sound level and spectrum monitoring disabled by default. Users need to actively call the relevant interface to enable monitoring.

  • Interface prototype:
/**
 * Start sound level monitoring
 */
virtual void startSoundLevelMonitor() = 0;

/**
 * Stop sound level monitoring
 */
virtual void stopSoundLevelMonitor() = 0;

/**
 * Start audio spectrum monitoring
 */
virtual void startAudioSpectrumMonitor() = 0;

/**
 * Stop audio spectrum monitoring
 */
virtual void stopAudioSpectrumMonitor() = 0;
  • Usage example:
engine->startSoundLevelMonitor();
engine->stopSoundLevelMonitor();
engine->startAudioSpectrumMonitor();
engine->stopAudioSpectrumMonitor();

Listen for Sound Level and Spectrum Callbacks

After enabling sound level and spectrum monitoring, the SDK will periodically notify users of the current sound level and spectrum data through relevant callbacks. Users only need to rewrite the relevant callback functions to implement UI display.

  • Interface prototype:
/**
 * Local capture audio sound level callback
 * @param soundLevel Locally captured sound level value, ranging from 0.0 to 100.0
 */
virtual void onCapturedSoundLevelUpdate(double soundLevel) {

}

/**
 * Remote audio sound level callback
 * @param soundLevels Remote sound level key-value pairs, where key is the stream ID and value is the corresponding stream's sound level value
 */
virtual void onRemoteSoundLevelUpdate(const std::map<std::string, double>& soundLevels) {

}

/**
 * Local capture audio spectrum callback
 * @param audioSpectrum Locally captured audio spectrum value array, spectrum values range from [0-2^30]
 *
 */
virtual void onCapturedAudioSpectrumUpdate(const ZegoAudioSpectrum& audioSpectrum) {

}

/**
 * Remote playing stream audio spectrum callback
 * @param audioSpectrums Remote audio spectrum key-value pairs, where key is the stream ID and value is the corresponding stream's audio spectrum value array, spectrum values range from [0-2^30]
 *
 */
virtual void onRemoteAudioSpectrumUpdate(const std::map<std::string, ZegoAudioSpectrum>& audioSpectrums) {

}
  • Usage example:
class MyEventHandler: public IZegoEventHandler
{
    virtual void onCapturedSoundLevelUpdate(double soundLevel) {
        printf("onCapturedSoundLevelUpdate");
        ... // operate ui
    }
    virtual void onRemoteSoundLevelUpdate(const std::map<std::string, double>& soundLevels) {
        printf("onRemoteSoundLevelUpdate");
        ... // operate ui
    }
    virtual void onCapturedAudioSpectrumUpdate(const ZegoAudioSpectrum& audioSpectrum) {
        printf("onCapturedAudioSpectrumUpdate");
        ... // operate ui
    }
    virtual void onRemoteAudioSpectrumUpdate(const std::map<std::string, ZegoAudioSpectrum>& audioSpectrums) {
        printf("onRemoteAudioSpectrumUpdate");
        ... // operate ui
    }
};

API Reference List

MethodDescription
startSoundLevelMonitor Start sound level monitoring
stopSoundLevelMonitor Stop sound level monitoring
startAudioSpectrumMonitor Start audio spectrum monitoring
stopAudioSpectrumMonitor Stop audio spectrum monitoring
onCapturedSoundLevelUpdate Local capture audio sound level callback
onRemoteSoundLevelUpdate Remote audio sound level callback
onCapturedAudioSpectrumUpdate Local capture audio spectrum callback
onRemoteAudioSpectrumUpdate Remote playing stream audio spectrum callback

Q&A

Q1: After enabling the sound level and spectrum monitoring switches, why are there no related callbacks? The local capture callback will be triggered immediately, and the callback value when not publishing is 0; The remote playing stream callback will only be triggered after successfully playing the stream startPlayingStream .

Previous

Login to Multiple Rooms

Next

Ear Monitor and Channel Settings

On this page

Back to top