

// Call the NTP time synchronization interface when entering the room.[TXLiveBase updateNetworkTime];// In the TXLiveBaseDelegate callback, determine whether the time synchronization is successful.- (void)onUpdateNetworkTime:(int)errCode message:(NSString *)errMsg {// errCode 0: 0: Time synchronization is successful and the deviation is within 30ms;// 1: Time synchronization is successful, but the deviation may be over 30ms;// -1: Time synchronization failed.if (errCode == 0) {// Call TXLiveBase's getNetworkTimestamp to obtain the NTP timestamp.NSInteger ntpTime = [TXLiveBase getNetworkTimestamp];} else {// Call updateNetworkTime again to initiate a time synchronization.[TXLiveBase updateNetworkTime];}}
NSDictionary *json = @{@"cmd": @"startChorus",// Scheduled time for a tutti.@"startPlayMusicTS": @(startTs),@"musicId": @"musicId",@"musicDuration": @(musicDuration),};NSString *jsonString = [self jsonStringFrom:json];[trtcCloud sendCustomMessage:jsonString reliable:NO];
- (void)onRecvCustomCmdMsgUserId:(NSString *)userId cmdID:(NSInteger)cmdId seq:(UInt32)seq message:(NSData *)message {NSString *msg = [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding];NSError *error;NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[msg dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableContainerserror:&error];NSObject *cmdObj = [json objectForKey:@"cmd"];NSInteger musicDuration = [[json objectForKey:@"musicDuration"] integerValue];NSString *cmd = (NSString *)cmdObj;// tutti commandif ([cmd isEqualToString:@"startChorus"]) {// tutti start timeNSObject *startPlayMusicTsObj = [json objectForKey:@"startPlayMusicTS"];NSString *musicId = [json objectForKey:@"musicId"];NSInteger startPlayMusicTs = ((NSNumber *)startPlayMusicTsObj).longLongValue;// The difference between the scheduled tutti time and the current time.NSInteger startDelayMS = labs(startPlayMusicTs - [TXLiveBase getNetworkTimestamp]);// Start preloading, and jump the song progress according to the difference// between the scheduled duet time and the current NTP time.NSDictionary *jsonDict = @{@"api": @"preloadMusic",@"params": @{@"musicId": @(musicId),@"path": path,@"startTimeMS": @(startDelayMS),}};NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:NULL];NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];[subCloud callExperimentalAPI:jsonString];// play musicTXAudioMusicParam *param = [[TXAudioMusicParam alloc] init];param.ID = musicId;param.path = url;param.loopCount = 0;param.publish = NO;[[subCloud getAudioEffectManager] startPlayMusic:param onStart:^(NSInteger errCode) {// star play callback} onProgress:^(NSInteger progressMs, NSInteger durationMs) {// lyric progress callback} onComplete:^(NSInteger errCode) {// play completely callback}];}}
self.startPlayChorusMusicTs; // The originally scheduled tutti time.// Current playback progressNSInteger currentProgress = [[self audioEffecManager] getMusicCurrentPosInMS:self.currentPlayMusicID];// The ideal playback time progress of the current song.NSInteger estimatedProgress = [TXLiveBase getNetworkTimestamp] - self.startPlayChorusMusicTs;if (estimatedProgress >= 0 && labs(currentProgress - estimatedProgress) > 50) {// When the playback progress exceeds 50ms, make adjustments.[[subCloud getAudioEffectManager] seekMusicToPosInMS:self.currentPlayMusicID pts:estimatedProgress];}
Feedback