logo
On this page

Get the Raw Audio Data

2024-01-02

Feature Introduction

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

Sample Source Code Download

Please refer to Download Sample Source Code to get the source code.

For related source code, please check the files in the "/ZegoExpressExample/Examples/AdvancedAudioProcessing/OriginalAudioDataAcquisition" directory.

Prerequisites

Before Get the Raw Audio Data, please ensure:

Usage Steps

1 Initialize SDK

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

2 Enable Get the Raw Audio Data function

Developers can call the startAudioDataObserver interface to enable audio data callback monitoring. The callback audio data type is ZegoAudioDataCallbackBitMask, including ZegoAudioDataCallbackBitMaskCaptured, ZegoAudioDataCallbackBitMaskPlayback, ZegoAudioDataCallbackBitMaskMixed, and ZegoAudioDataCallbackBitMaskPlayer. All four callback types need to be enabled.

// Bitmask for required audio data types, this example enables all four callbacks
ZegoAudioDataCallbackBitMask bitmask = ZegoAudioDataCallbackBitMaskCaptured | ZegoAudioDataCallbackBitMaskPlayback | ZegoAudioDataCallbackBitMaskMixed | ZegoAudioDataCallbackBitMaskPlayer;

// Required audio data parameters, this example is mono channel, 16 K
ZegoAudioFrameParam *param = [[ZegoAudioFrameParam alloc] init];
param.channel = ZegoAudioChannelMono;
param.sampleRate = ZegoAudioSampleRate16K;

// Enable Get the Raw Audio Data function
[[ZegoExpressEngine sharedEngine] startAudioDataObserver:bitmask param:param];
Notes

If needed, please contact ZEGOCLOUD Technical Support.

3 Set callback to Get the Raw Audio Data and process

Developers can call the setAudioDataHandler interface to set additional callbacks for receiving audio data. As needed, you can implement callbacks onCapturedAudioData, onPlaybackAudioData, onMixedAudioData, and onPlayerAudioData, which correspond to the four audio data types in the above ZegoAudioDataCallbackBitMask respectively.

// Set audio data callback
[[ZegoExpressEngine sharedEngine] setAudioDataHandler:self];
// Implement the following four callbacks as needed, corresponding to the four options of the above Bitmask

- (void)onCapturedAudioData:(const unsigned char *)data dataLength:(unsigned int)dataLength param:(ZegoAudioFrameParam *)param {
    // Local captured audio data, callback can be received after publishing stream
}

- (void)onPlaybackAudioData:(const unsigned char *)data dataLength:(unsigned int)dataLength param:(ZegoAudioFrameParam *)param {
    // Audio data played by SDK. When the engine is in a non-playing stream state and not using media player to play media files, the callback audio data is silent audio data
}

- (void)onMixedAudioData:(const unsigned char *)data dataLength:(unsigned int)dataLength param:(ZegoAudioFrameParam *)param {
    // Audio data callback after mixing local captured audio and SDK played audio
}

- (void)onPlayerAudioData:(const unsigned char *)data dataLength:(unsigned int)dataLength param:(ZegoAudioFrameParam *)param streamID:(NSString *)streamID {
    // Remote playing stream audio data, callback for each playing stream data after starting playing stream
}

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

Stream Mixing

On this page

Back to top