All Products
Search
Document Center

ApsaraVideo Live:Android SDK integration

Last Updated:Aug 02, 2025

This topic describes how to integrate the SDK for voice chat on Android and provides relevant sample code.

Prerequisites

For instructions on how to prepare an ARTC application and integrate the SDK, see Implement an audio and video call on an Android client. This topic describes the development steps and features for implementing a voice chat room.

Implementation diagram

image.png

Feature implementation

Streamer creates a channel

mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
mAliRtcEngine.setClientRole(AliRTCSdkInteractive);
/* We recommend that you use this in scenarios that require high-quality music. */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneMusicMode);

/* Set the listener for callbacks. */
mAliRtcEngine.setRtcEngineEventListener(this);
//auto pub
mAliRtcEngine.publishLocalAudioStream(true);
//auto sub
mAliRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(true);
mAliRtcEngine.subscribeAllRemoteAudioStreams(true);
mAliRtcEngine.setAudioOnlyMode(true);

JSONObject tokenv2 = new JSONObject();
tokenv2.put("appid", userInfo.appId);
tokenv2.put("channelid", userInfo.channelId);
tokenv2.put("userid", userInfo.userId);
tokenv2.put("nonce", userInfo.nonce);
tokenv2.put("timestamp", userInfo.timestamp);
tokenv2.put("gslb",userInfo.gslb);
tokenv2.put("token", userInfo.token);
String base64TokenV2 = Base64.encodeToString(JSON.toJSONBytes(tokenv2),Base64.NO_WRAP);
/* Join the channel. */
mAliRtcEngine.joinChannel(base64TokenV2, null, null, mUsername);

Viewer joins a channel

The methods called to let a viewer join the room are the same as the methods called to let the streamer create a room. Specify the role based on whether the viewer is an ordinary viewer or co-streamer. The following sample code provides an example when the viewer is an ordinary viewer.

mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
/* The viewer role. */
mAliRtcEngine.setClientRole(AliRTCSdkLive);
/* We recommend that you use this in scenarios that require high-quality music. */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneMusicMode);
/* Set the listener for callbacks. */
mAliRtcEngine.setRtcEngineEventListener(this);
//auto pub
mAliRtcEngine.publishLocalAudioStream(true);
//auto sub
mAliRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(true);
mAliRtcEngine.subscribeAllRemoteAudioStreams(true);
mAliRtcEngine.setAudioOnlyMode(true);

JSONObject tokenv2 = new JSONObject();
tokenv2.put("appid", userInfo.appId);
tokenv2.put("channelid", userInfo.channelId);
tokenv2.put("userid", userInfo.userId);
tokenv2.put("nonce", userInfo.nonce);
tokenv2.put("timestamp", userInfo.timestamp);
tokenv2.put("gslb",userInfo.gslb);
tokenv2.put("token", userInfo.token);
String base64TokenV2 = Base64.encodeToString(JSON.toJSONBytes(tokenv2),Base64.NO_WRAP);
/* Join the channel. */
mAliRtcEngine.joinChannel(base64TokenV2, null, null, mUsername);

Viewer switches the role

/* Switch the role to streamer for a user who has joined the channel. */
mAliRtcEngine.setClientRole(AliRTCSdkLive);

Set the volume, voice changer, and reverberation

/* Adjust the local recording volume. */
mAliRtcEngine.setRecordingVolume(volumeLevel);
/* Adjust the volume of a specific remote user. */
mAliRtcEngine.setRemoteAudioVolume(uid, volume);
/* Adjust the playback volume of all sounds. */
mAliRtcEngine.setPlayoutVolume(uid, volume);
/* Set the voice changer. */
mAliRtcEngine.setAudioEffectVoiceChangerMode(AliRtcSdk_AudioEffect_Voice_Changer_Oldman);
/* Set the reverberation. */
mAliRtcEngine.setAudioEffectReverbParamType(AliRtcEngine.AliRtcAudioEffectReverbParamType.AliRtcSdk_AudioEffect_Reverb_Room_Size, value);

Play background music

PCM data entry

Create a music input
AliRtcEngine.AliRtcExternalAudioStreamConfig config = new AliRtcEngine.AliRtcExternalAudioStreamConfig();
config.sampleRate = sampleRate;
config.channels = channels;
/* The local playback volume of the accompaniment. To use the ARTC SDK to implement playback, you can set this value. */
config.playoutVolume = 60;
config.publishVolume = 100;
int externalAudioStreamId = aliRtcEngine.addExternalAudioStream(config);
Enter PCM data
/* Push the PCM data. */ 
AliRtcEngine.AliRtcAudioFrame sample = new AliRtcEngine.AliRtcAudioFrame();
sample.data = buffer;
sample.numSamples = numSamples;
sample.numChannels = channels;
sample.sampleRate = sampleRate;
sample.bytesPerSample = bytesPerSample;
int ret = aliRtcEngine.pushExternalAudioStreamRawData(externalAudioStreamId, sample);

File input

AliRtcEngine.AliRtcAudioAccompanyConfig config = new AliRtcEngine.AliRtcAudioAccompanyConfig();
config.onlyLocalPlay = localPlay;
config.replaceMic = replaceMic;
config.loopCycles = loopCycles;
config.startPosMs = startPosMs;
config.publishVolume = pubVolume;
config.playoutVolume = playVolume;
return mAliRtcEngine.startAudioAccompany(audioFileName, config);