Android SDK集成

本文介绍语聊房Android SDK集成的代码示例及集成说明。

功能时序图

image.png

主播创建房间

mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
mAliRtcEngine.setClientRole(AliRTCSdkInteractive);
/* 建议使用高音质和音乐场景 */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneMusicMode);

/* 设置回调的监听 */
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);
/* 入会 */
mAliRtcEngine.joinChannel(base64TokenV2, null, null, mUsername);

观众加入房间

和主播创建房间调用接口相同,根据是否上麦,设置角色信息,以观众为示例:

mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
/* 观众角色 */
mAliRtcEngine.setClientRole(AliRTCSdkLive);
/* 建议使用高音质和音乐场景 */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneMusicMode);
/* 设置回调的监听 */
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);
/* 入会 */
mAliRtcEngine.joinChannel(base64TokenV2, null, null, mUsername);

观众切换角色

/* 之前已经入会,切换为主播角色 */
mAliRtcEngine.setClientRole(AliRTCSdkLive);

设置音量、变声、混响

/* 调整本人采集音量 */
mAliRtcEngine.setRecordingVolume(volumeLevel);
/* 调整远端特定user的音量 */
mAliRtcEngine.setRemoteAudioVolume(uid, volume);
/* 调整所有声音的播放音量 */
mAliRtcEngine.setPlayoutVolume(uid, volume);
/* 设置变声器 */
mAliRtcEngine.setAudioEffectVoiceChangerMode(AliRtcSdk_AudioEffect_Voice_Changer_Oldman);
/* 设置混响 */
mAliRtcEngine.setAudioEffectReverbParamType(AliRtcEngine.AliRtcAudioEffectReverbParamType.AliRtcSdk_AudioEffect_Reverb_Room_Size, value);

播放背景音乐

pcm数据输入

创建音乐输入

AliRtcEngine.AliRtcExternalAudioStreamConfig config = new AliRtcEngine.AliRtcExternalAudioStreamConfig();
config.sampleRate = sampleRate;
config.channels = channels;
/* 伴奏音乐本地播放的音量,如果需要rtc sdk来实现播放,可以设置该值 */
config.playoutVolume = 60;
config.publishVolume = 100;
int externalAudioStreamId = aliRtcEngine.addExternalAudioStream(config);

输入pcm

/* 送入pcm数据 */ 
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);

文件输入

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);