All Products
Search
Document Center

ApsaraVideo Live:Custom audio playback

Last Updated:Nov 04, 2025

ApsaraVideo Real-time Communication (ARTC) provides the custom audio playback feature. It is designed for users with some development capabilities who want to control playback to meet specific requirements.

Feature introduction

By default, ARTC includes a marketplace-tested audio playback module that is suitable for most scenarios. However, in certain scenarios, you might have a mature custom audio playback module or need to process audio data before playback. In these cases, ARTC provides a custom audio playback feature. This feature lets you disable the internal playback logic and manage the audio data receiving and playback procedure yourself.

The core of this feature is to enable audio frame callbacks to obtain remote or local pulse-code modulation (PCM) audio data. This data is then passed to your custom playback device for processing and playback.

Prerequisites

Before using the custom audio playback feature, ensure that you meet the following prerequisites:

  • You have the development experience to create a complete audio playback module, or you have a mature third-party or self-developed audio player.

  • You understand the basic audio processing flow, including concepts such as PCM data format, sample rate, and the number of sound channels.

  • You have integrated the ARTC software development kit (SDK) and implemented basic audio and video call features.

  • Your business requirements must necessitate bypassing the internal ARTC playback module. Otherwise, use the default playback solution to ensure stability and compatibility.

Implementation

image

Because the implementation details differ between iOS and Android, the following sections describe the implementation solutions of custom audio playback for iOS and Android, respectively.

Implementation on iOS

1. Configure playback

Configure the relevant parameter: "enable_system_audio_device_play"="TRUE".

/* Dynamically disable internal ARTC playback */
[_engine setParameter:@"{\"audio\":{\"enable_system_audio_device_play\":\"FALSE\"}}"];

/* Dynamically enable internal ARTC playback */
[_engine setParameter:@"{\"audio\":{\"enable_system_audio_device_play\":\"TRUE\"}}"];

2. Listen to audio callbacks

Call enableAudioFrameObserver to register the audio callback listener.

AliRtcAudioFrameObserverConfig* config = [[AliRtcAudioFrameObserverConfig alloc] init];

config.sampleRate = AliRtcAudioSampleRate_Unknown ;

/* AudioDeviceIos is the playback module that you need to implement. This is sample code. */
audio_device_ = new AudioDeviceIos() ;

audio_device_->StartPlayout() ;
 
[_engine enableAudioFrameObserver:is_on audioSource:AliRtcAudioSourcePlayback config:config];

3. Process received PCM data for the system audio device in the callback

Process the received pulse-code modulation (PCM) data in the callback and submit it to the audio device.

- (BOOL)onPlaybackAudioFrame:(AliRtcAudioFrame* _Nonnull)frame {

    audio_device_->SyncWriteToBuffer(frame) ;
    return true;
}

4. Leave the channel and stop listening

[_engine enableAudioFrameObserver:false audioSource:AliRtcAudioSourcePlayback config:config];
audio_device_->StopPlayout();

Implementation on Android

1. Configure playback

Configure the relevant parameter: "enable_system_audio_device_play"="TRUE".

/* Dynamically disable internal ARTC playback */
String parameter = "{\"audio\":{\"enable_system_audio_device_play\":\"FALSE\"}}";
mAliRtcEngine.setParameter(parameter);

/* Dynamically enable internal ARTC playback */
String parameter = "{\"audio\":{\"enable_system_audio_device_play\":\"TRUE\"}}"; 
mAliRtcEngine.setParameter(parameter);

2. Listen to audio callbacks

Call enableAudioFrameObserver to register the audio callback listener.

/*
Set the callback.
*/
AliRtcEngine.AliRtcAudioFrameObserverConfig config = new AliRtcEngine.AliRtcAudioFrameObserverConfig();

config.sampleRate = AliRtcAudioSampleRate_Unknown ;

/* You need to implement AudioDeviceAndroid. */
audio_device_ = new AudioDeviceAndroid() ;

audio_device_.StartPlayout() ;
 
mAliRtcEngine.enableAudioFrameObserver(true, AliRtcAudioSourcePlayback, config);

3. Process received PCM data for the system audio device in the callback

Process the received PCM data in the callback and submit it to the audio device.

/*
 Write to the system audio driver.
*/

@Override
 public boolean onPlaybackAudioFrame(AliRtcEngine.AliRtcAudioFrame aliAudioSample) {
   audio_device_.SyncWriteToBuffer(aliAudioSample) ;
   return true ;
 }
 

4. Leave the channel and stop listening

mAliRtcEngine.enableAudioFrameObserver(false , AliRtcAudioSourcePlayback , config);
audio_device_.StopPlayout();