logo
Video Call
Other Features
On this page

Audio Mixing

2024-01-30

Feature Overview

Audio mixing refers to the SDK obtaining a stream of audio data from the App, and integrating the audio data provided by the App with the audio data collected by the SDK into one stream of audio data; to achieve the requirement of playing custom sounds or music files during calls or live streaming and letting others in the room hear them.

The audio mixing function is commonly used in the following scenarios:

During live streaming, sound effects such as applause and whistles are needed, or background music needs to be played.

Usage Steps

Enable/Disable Audio Mixing

The SDK disables the audio mixing function by default, and users need to actively call related interfaces to enable this function.

  • Interface prototype:
/**
 * Enable/Disable audio mixing function
 *
 * Enable audio mixing, combined with setAudioMixingHandler, to provide the SDK with audio data for mixing
 *
 * @param enable Whether to enable audio mixing function; true means enable; false means disable
 */
virtual void enableAudioMixing(bool enable) = 0;
  • Call example:
engine->enableAudioMixing(true);

Pass Audio Mixing Data to SDK

After enabling the audio mixing function, the SDK will trigger a callback when audio mixing data is needed. Users need to pass audio mixing data to the SDK in this callback.

/**
 * Set audio mixing related callback
 *
 * @param handler Audio mixing callback
 */
virtual void setAudioMixingHandler(std::shared_ptr<IZegoAudioMixingHandler> handler) = 0;
  • Call example:
class MyAudioMixingHandler: public IZegoAudioMixingHandler{
    /**
    * Copy audio mixing PCM data into the SDK, used to mix the audio data provided by developers into the published stream audio data. Used in combination with [enableAudioMixing]
    *
    * Supports PCM audio data with sampling rates of 16k, 32k, 44.1k, 48k, mono or stereo, 16-bit depth
    * This is a high-frequency callback. To ensure audio mixing quality, please do not perform time-consuming operations in this callback
    *
    * @param data Audio mixing data
    */
    void onAudioMixingCopyData(ZegoAudioMixingData* data) override{

        // Please note, do not call any SDK interfaces in the SDK callback thread, you need to manually switch to another thread, otherwise a deadlock will occur
        // Please fill in according to the actual loaded PCM format here
        data->param.sampleRate = ZEGO_AUDIO_SAMPLE_RATE_44K;
        data->param.channel = ZEGO_AUDIO_CHANNEL_STEREO;

        // When the actual PCM length to be written to the SDK meets the SDK requirements
        if(pcmData->size() >= data->audioDataLength)
        {
            memcpy(data->audioData, pcmData, data->audioDataLength);
        }
        // When the actual PCM length to be written to the SDK does not meet the SDK requirements
        else
        {
            memcpy(data->audioData, pcmData, pcmData->size());
            data->audioDataLength = 0;
        }
    }
};

engine->setAudioMixingHandler(std::make_shared<MyAudioMixingHandler>());

The audioDataLength in ZegoAudioMixingData is both an input parameter and an output parameter; As an input parameter, the SDK will provide a length value, and users can write data according to this length. When the data is sufficient, there is no need to change the value of audioDataLength. As an output parameter, if the filled data is less than the length provided by the SDK, you can set audioDataLength = 0, or when the final tail sound is less than the length provided by the SDK, it can be filled with mute data.

Set Audio Mixing Volume

After enabling the audio mixing function, developers can call setAudioMixingVolume to adjust the volume of the mixed audio as needed; or call muteLocalAudioMixing to set the audio mixing to mute. After muting, the anchor side will not hear the audio mixing sound, but the audience side can still hear the audio mixing sound.

  • Interface prototype:
/**
 * Set audio mixing volume
 *
 * This API can separately set the local playback audio mixing volume or the audio mixing volume mixed into the published stream
 *
 * @param volume Audio mixing volume, ranging from 0 to 200, default is 100
 * @param type Audio mixing local playback volume / Audio mixing volume in published stream
 */
virtual void setAudioMixingVolume(int volume, ZegoVolumeType type) = 0;

/**
 * Mute or restore local audio mixing playback
 *
 * After muting audio mixing playback, the audio mixing side (publishing side) will not hear the audio mixing playback sound, but the remote side (playing side) can still hear the audio mixing
 *
 * @param mute Whether to mute local audio mixing; true means prohibit playback; false means enable
 */
virtual void muteLocalAudioMixing(bool mute) = 0;
  • Call example:
engine->setAudioMixingVolume(80, ZEGO_VOLUME_TYPE_LOCAL);
engine->setAudioMixingVolume(20, ZEGO_VOLUME_TYPE_REMOTE);

engine->muteLocalAudioMixing(true);

API Reference List

MethodDescription
enableAudioMixingEnable/Disable audio mixing
setAudioMixingHandlerSet audio mixing callback
setAudioMixingVolume Set audio mixing volume
muteLocalAudioMixing Mute audio mixing local playback

Previous

Voice Changer/Reverb/Stereo

Next

Scenario-Based AI Noise Reduction

On this page

Back to top