本文介绍K歌房单人演唱模式Android SDK集成的代码示例及集成说明。
功能时序图
主播创建房间
mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
mAliRtcEngine.setClientRole(AliRTCSdkInteractive);
/* 使用高音质和ktv场景 */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneKtvMode);
//auto pub
mAliRtcEngine.publishLocalAudioStream(true);
/* 设置回调的监听 */
mAliRtcEngine.setRtcEngineEventListener(this);
//auto sub
mAliRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(true);
mAliRtcEngine.subscribeAllRemoteAudioStreams(true);
mAliRtcEngine.setAudioOnlyMode(true);
mAliRtcEngine.enableSpeakerphone(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);
/* 打开datachannel 用于接收歌曲进度 */
String param = "\"data\":{\"enablePubDataChannel\":true" + ",\"enableSubDataChannel\":true}";
param = "{" + param + "}";
mAliRtcEngine.setParameter(param);
观众加入房间
和主播创建房间调用接口相同,根据是否上麦,设置角色信息,以观众为示例:
mAliRtcEngine.setChannelProfile(AliRTCSdkInteractiveLive);
/* 观众角色 */
mAliRtcEngine.setClientRole(AliRTCSdkLive);
/* 使用高音质和ktv场景 */
mAliRtcEngine.setAudioProfile(AliRtcEngineHighQualityMode, AliRtcSceneKtvMode);
//auto pub
mAliRtcEngine.publishLocalAudioStream(true);
/* 设置回调的监听 */
mAliRtcEngine.setRtcEngineEventListener(this);
//auto sub
mAliRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(true);
mAliRtcEngine.subscribeAllRemoteAudioStreams(true);
mAliRtcEngine.setAudioOnlyMode(true);
mAliRtcEngine.enableSpeakerphone(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);
/* 打开datachannel 用于接收歌曲进度 */
String param = "\"data\":{\"enablePubDataChannel\":true" + ",\"enableSubDataChannel\":true}";
param = "{" + param + "}";
mAliRtcEngine.setParameter(param);
演唱者点歌开始演唱
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数据 */
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);
/* 发送datachannel 进度信息 */
AliRtcEngine.AliRtcDataChannelMsg msg = new AliRtcEngine.AliRtcDataChannelMsg();
msg.type = AliRtcEngine.AliRtcDataMsgType.AliEngineDataMsgCustom;
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
long curTime = mAliRtcEngine.getNetworkTime();
msg.progress = getProgress();
buffer.putLong(curTime);
msg.data = buffer.array();
mAliRtcEngine.sendDataChannelMsg(msg);
观众侧接收歌曲进度
public void onDataChannelMessage(String uid, AliRtcEngine.AliRtcDataChannelMsg controlMsg) {
super.onDataChannelMessage(uid, controlMsg);
Log.d(TAG, "datachannel type=" + controlMsg.type + ", time=" + controlMsg.networkTime + ", progress=" + controlMsg.progress);
}
主播停止歌曲
mAliRtcEngine.removeExternalAudioStream(externalAudioStreamId);
/* 停止歌曲解码、IM通知其他人 */
文档内容是否对您有帮助?