logo
On this page

Get the Raw Audio Data

2024-01-02

Feature Overview

The SDK provides developers with the function of obtaining raw audio data. The format of the obtained raw audio data is PCM. Developers can write this data to local devices to implement audio recording.

Usage Steps

The usage flow for Get the Raw Audio Data is as follows.

1 Initialize SDK

Please refer to "Create Engine" in Quick Start - Implementation Flow.

2 Enable Raw Audio Data Function

Developers can call the startAudioDataObserver interface to enable audio data callback monitoring. The audio data type of the callback is ZegoAudioDataCallbackBitMask, including ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_CAPTURED, ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYBACK, ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_MIXED, ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYER. All four callback types need to be enabled.

// Enable Get the Raw Audio Data function
ZegoAudioFrameParam param;
param.channel = ZEGO_AUDIO_CHANNEL_STEREO;
param.sampleRate = ZEGO_AUDIO_SAMPLE_RATE_8K;
unsigned int bitmask = ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_CAPTURED | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYBACK | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_MIXED | ZEGO_AUDIO_DATA_CALLBACK_BIT_MASK_PLAYER;
engine->startAudioDataObserver(bitmask, param);
Note

If needed, please contact ZEGOCLOUD Technical Support.

3 Set Callback to Receive Raw Audio Data and Process

Developers can call the setAudioDataHandler interface to set an additional callback for receiving audio data. According to needs, implement callbacks onCapturedAudioData, onPlaybackAudioData, onMixedAudioData, onPlayerAudioData, corresponding to the four audio data types in ZegoAudioDataCallbackBitMask above.

// Set raw audio data callback
// 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
class MyAudioDataHandler: public IZegoAudioDataHandler
{
public:
    // Local captured audio data, callback can be received after publishing stream
    virtual void onCapturedAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Remote playing stream audio data, callback can be received after starting playing stream
    virtual void onPlaybackAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Audio data callback after mixing local captured and remote playing stream sounds
    virtual void onMixedAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/) {

    }

    // Remote playing stream audio data, callback for each playing stream data after starting playing stream
    virtual void onPlayerAudioData(const unsigned char* /*data*/, unsigned int /*dataLength*/, ZegoAudioFrameParam /*param*/, const std::string& /*streamID*/) {

    }
};

engine->setAudioDataHandler(std::make_shared<MyAudioDataHandler>());

4 Stop Audio Data Callback Monitoring

If you want to stop audio data callback monitoring, you can call the stopAudioDataObserver interface.

[[ZegoExpressEngine sharedEngine] stopAudioDataObserver];

Previous

Voice Changer/Reverb/Stereo

Next

Using CDN for Live Streaming

On this page

Back to top