Video Call
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • React Native
  • Electron
  • Unity3D
  • Cocos Creator
  • Windows
  • macOS
  • Linux
  • Overview
  • Develop your app
    • Integrate the SDK
    • Implement a basic video call
    • Enhance basic feature
      • Use Tokens for authentication
      • Config your video based on scenes
      • Check the room connection status
      • Set up common video config
      • Set up common audio config
  • Best practices
    • Implement a video call for multiple users
  • Upgrade using advanced features
    • Advanced features
      • Configure the video
        • Watermark the video/Take snapshots
        • Improve your appearance in the video
        • Beautify & Change the voice
        • Configure video codec
        • Output the video in H.265
      • Improve video quality
        • Configure bandwidth management
        • Test network and devices in advance
        • Visualize the sound level
        • Monitor streaming quality
      • Message signaling
        • Convey extra information using SEI
        • Broadcast real-time messages to a room
        • Quotas and limits
      • Play media files
        • Play media files
        • Play sound effects
      • Share the screen
      • Mix the video streams
      • Publish multiple video streams
      • Encrypt the video streams
      • Record video media data
    • Distincitve features
      • Join multiple rooms
      • Customize the video and audio
      • Set the voice hearing range
      • Use the bit mask
      • Play streams via URL
      • Play a transparent gift special effect
  • Upgrade using Add-on
  • Resources & Reference
    • SDK
    • Sample codes
    • API reference
      • Client APIs
      • Server APIs
    • Debugging
      • Error codes
      • Logging/Version number
    • FAQs
    • Key concepts
  • Documentation
  • Video Call
  • Upgrade using advanced features
  • Advanced features
  • Improve video quality
  • Visualize the sound level

Visualize the sound level

Last updated:2023-05-17 19:00

Introduction

Sound level refers to the volume of a stream. ZEGOCLOUD’s SDKs provide the ability to capture the sound level of a stream in real time and deliver the captured sound level data to the app client through related callbacks. A typical use case of this feature is that you can visualize the sound level data on your app UI to indicate the current active speaker and their volume. The following picture shows an example of such use cases.

Audio spectrum refers to a series of values, each of which represents an audio signal's energy level at a point in a frequency range. ZEGOCLOUD’s SDKs provide the ability to capture the audio spectrum data of a stream in real time and deliver the captured audio spectrum data to the app client through related callbacks. A typical use case of this feature is that you can use it to create and display an audio spectrum visualizer on your app UI, especially for those online karaoke apps. The following picture shows an example of audio spectrum visualizers.

Prerequisites

Before you begin, make sure you complete the following:

  • Create a project in ZEGOCLOUD Admin Console and get the AppID and AppSign of your project.

  • Refer to the Quick Start doc to complete the SDK integration and basic function implementation.

Enable the sound level callbacks

To enable sound level callbacks, call the startSoundLevelMonitor method.

/// Enable sound level callbacks.
///
- (void)startSoundLevelMonitor;

/// Enable the sound level monitor, supports enabling advanced features.
///
/// @note After enabled the monitor, the SDK sends out notification through the callback [onCapturedSoundLevelUpdate] to deliver the sound level data of locally captured audio, and through the callback [onRemoteSoundLevelUpdate] to deliver sound level data of remote streams.
/// @note You can call the [startPreview] method before entering a room, and together with the callback [onCapturedSoundLevelUpdate] to check whether the audio device is working properly. 
/// @note The notification interval of the [onCapturedSoundLevelUpdate] and [onRemoteSoundLevelUpdate] callbacks is the value of the parameter.
///
/// @param config The configuration to enable sound level monitor. 
- (void)startSoundLevelMonitorWithConfig:(ZegoSoundLevelConfig *)config;

After the above step is completed:

  • When you start previewing the local video (by calling the startPreview method) or publishing a local stream (by calling the startPublishingStream method), the SDK will trigger the onCapturedSoundLevelUpdate callback at an interval of 100ms to deliver the sound level data of the locally captured audio.
/// The callback to deliver the sound level data of locally captured audio.
///
/// @param soundLevel: The sound level value of the locally captured audio, which is in the range [0.0, 100.0].
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel;

/// The callback to deliver the sound level data of locally captured audio.
///
/// @note To trigger this callback, you must call the [startSoundLevelMonitor] method to enable the sound level monitor.
/// @note The callback notification interval is the value set when [startSoundLevelMonitor] is called.
///
/// @param soundLevelInfo The sound level value of the locally captured audio, which is in the range [0.0, 100.0].
- (void)onCapturedSoundLevelInfoUpdate:(ZegoSoundLevelInfo *)soundLevelInfo;               
  • When you start playing remote streams (by calling the startPlayingStream method), the SDK will trigger the onRemoteSoundLevelUpdate callback at an interval of 100ms to deliver the sound level data of the remote streams.
/// The callback to deliver the sound level data of remote streams.
///
/// @param soundLevels: The sound level data (key-value pairs) of the remote streams, of which the key is a stream ID, and the value is the sound level value of the corresponding stream. Each sound level value is in the range [0.0, 100.0].
- (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *, NSNumber *> *)soundLevels;

/// The callback to deliver the sound level data of remote streams when stream playing.
///
/// @note To trigger this callback, you must call the [startSoundLevelMonitor] method to enable the sound level monitor when playing streams. 
/// @note The callback notification interval is the value set when [startSoundLevelMonitor] is called.
///
/// @param soundLevelInfos The sound level data (key-value pairs) of the remote streams, of which the key is a stream ID, and the value is the sound level value of the corresponding stream. Each sound level value is in the range [0.0, 100.0].
- (void)onRemoteSoundLevelInfoUpdate:(NSDictionary<NSString *, ZegoSoundLevelInfo *> *)soundLevelInfos;

Enable the audio spectrum callbacks

To enable audio spectrum callbacks, call the startAudioSpectrumMonitor method.

/// Enable audio spectrum callbacks
///
- (void)startAudioSpectrumMonitor;

After the above step is completed:

/// The callback to deliver the audio spectrum data of locally captured audio.
///
/// @param audioSpectrum: An array of audio spectrum values of the locally captured audio. Each audio spectrum value is in the range [0, 2^30].
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum;
/// The callback to deliver the audio spectrum data of remote streams
///
/// @param audioSpectrums: The audio spectrum data (key-value pairs) of remote streams, of which the key is a stream ID, and the value is an array of audio spectrum values of the corresponding stream. Each audio spectrum value is in the range [0, 2^30].
- (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *, NSArray<NSNumber *> *> *)audioSpectrums;

Obtain data from the callbacks

The SDK delivers the sound level data and audio spectrum data of the remote streams as key-value pairs in an NSDictionary. In each key-value pair, the key is the stream ID of a remote stream published by another user in the same room, and the value is the sound level value or an array of audio spectrum values that stream.

You can first obtain and save the list of remote streams published by the other users in the current room through the onRoomStreamUpdate callback, and then use each stream ID in the list as an index to get the sound level value or the audio spectrum data of that stream from the NSDictionary.

The following examples show how to obtain the sound level and audio spectrum data from the callbacks.

// The callback to deliver the sound level data of locally captured audio.
- (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel {
    ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.soundLevel = soundLevel;
}

// The callback to deliver the sound level data (key-value pairs) of remote streams. In each key-value pair, the key is the stream ID of a remote stream, and the value is the sound level data of that stream.
- (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *,NSNumber *> *)soundLevels {
    NSInteger rowCount = [self.tableView numberOfRowsInSection:1];
    for (NSInteger row = 0; row < rowCount; row++) {
        ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]];
        if ([soundLevels objectForKey:cell.streamID]) {
            cell.soundLevel = soundLevels[cell.streamID];
        }
    }
}

// The callback to deliver the audio spectrum data of locally captured audio.
- (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum {
    ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.spectrumList = audioSpectrum;
}

// The callback to deliver the audio spectrum data (key-value pairs) of the remote streams. Audio spectrum callback for remote streams. In each key-value pair, the key is the stream ID of a remote stream, and the value is the audio spectrum data of that stream.
- (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *,NSArray<NSNumber *> *> *)audioSpectrums {
    NSInteger rowCount = [self.tableView numberOfRowsInSection:1];
    for (NSInteger row = 0; row < rowCount; row++) {
        ZGSoundLevelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]];
        if ([audioSpectrums objectForKey:cell.streamID]) {
            cell.spectrumList = audioSpectrums[cell.streamID];
        }
    }
}

Disable the sound level callbacks

To disable the sound level callbacks, call the stopSoundLevelMonitor method.

/// Disable sound level callbacks
- (void)stopSoundLevelMonitor;

After the above step is completed, the SDK will stop triggering the callbacks onCapturedSoundLevelUpdate and onRemoteSoundLevelUpdate.

Disable the audio spectrum callbacks

To disable audio spectrum callbacks, call the stopAudioSpectrumMonitor method.

/// Stop listening for audio spectrum callbacks
///
- (void)stopAudioSpectrumMonitor;

After the above step is completed, the SDK will stop triggering the callbacks onCapturedAudioSpectrumUpdate and onRemoteAudioSpectrumUpdate.

FAQ

  1. Why I receive no sound level callbacks or audio spectrum callbacks after I enable them?

    After you enable the sound level callbacks and audio spectrum callbacks, the callbacks related to locally captured audio are triggered at the same time. The callback returns a value of 0 when no streams are published.

    And the SDK only triggers the callbacks when remote users start playing streams with the startPlayingStream method.

Page Directory