// Create TRTCCloud main instance (vocal instance)TRTCCloud *trtcCloud = [TRTCCloud sharedInstance];// Create TRTCCloud sub-instance (accompaniment instance)TRTCCloud *subCloud = [trtcCloud createSubCloud];
TRTCParams *params = [[TRTCParams alloc] init];params.sdkAppId = sdkAppId;params.userId = userId;params.userSig = userSign;params.role = TRTCRoleAnchor;params.roomId = roomIdIntValue;[trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];// Turn on audio uplink and set audio quality[trtcCloud startLocalAudio:TRTCAudioQualityMusic];// Set media type[trtcCloud setSystemVolumeType:TRTCSystemVolumeTypeMedia];// Mute remote accompaniment music[trtcCloud muteRemoteAudio:remoteAudioId mute:YES];
TRTCParams *bgmParams = [[TRTCParams alloc] init];bgmParams.sdkAppId = sdkAppId;bgmParams.userId = [NSString stringWithFormat:@"%@%@",userId,@"_bgm"];bgmParams.userSig = bgmUserSign;bgmParams.role = TRTCRoleAnchor;bgmParams.roomId = roomIdIntValue;[subCloud enterRoom:bgmParams appScene:TRTCAppSceneLIVE];// Set media type[subCloud setSystemVolumeType:TRTCSystemVolumeTypeMedia];// Enable preloadingNSDictionary *jsonDict = @{@"api": @"preloadMusic",@"params": @{@"musicId": @(self.currentPlayMusicID),@"path": path,@"startTimeMS": @(startMs),}};NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:NULL];NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];[subCloud callExperimentalAPI:jsonString];// Play accompaniment music and push the stream (play at the agreed time)TXAudioMusicParam *musicParam = [[TXAudioMusicParam alloc] init];musicParam.ID = musicID;musicParam.path = url;musicParam.loopCount = 0;musicParam.publish = YES;// Send accompaniment music to the remote endparam.publish = YES;[[subCloud getAudioEffectManager] startPlayMusic:musicParam onStart:startBlock onProgress:progressBlock onComplete:completedBlock]
// Create a TRTCPublishTarget objectTRTCPublishTarget *publishTarget = [[TRTCPublishTarget alloc] init];// Push back to the room after mixing, if publishing to CDN, fill in TRTCPublishMixStreamToCdnpublishTarget.mode = TRTCPublishMixStreamToRoom;// The userid of the mixing robot, which cannot be duplicated with other users' userid in the roompublishTarget.mixStreamIdentity = [NSString stringWithFormat:@"%@%@",userId,@"_mix"];// Set the encoding parameters of the transcoded audio streamTRTCStreamEncoderParam *streamEncoderParam = [[TRTCStreamEncoderParam alloc] init];streamEncoderParam.videoEncodedFPS = 15;streamEncoderParam.videoEncodedGOP = 3;streamEncoderParam.videoEncodedKbps = 30;streamEncoderParam.audioEncodedSampleRate = 48000;streamEncoderParam.audioEncodedChannelNum = 2;streamEncoderParam.audioEncodedKbps = 64;streamEncoderParam.audioEncodedCodecType = 2;// Set audio mixing parametersTRTCStreamMixingConfig *streamMixingConfig = [[TRTCStreamMixingConfig alloc] init];// Support filling in empty values, which will automatically mix the audio of all hosts and outputstreamMixingConfig.audioMixUserList = @[];// Initiate mixed stream transcoding and pushing request[trtcCloud startPublishMediaStream:publishTarget encoderParam:streamEncoderParam mixingConfig:streamMixingConfig];
Was this page helpful?