

//进房时调用NTP校时接口[TXLiveBase updateNetworkTime];// TXLiveBaseDelegate 回调内判断是否校时成功- (void)onUpdateNetworkTime:(int)errCode message:(NSString *)errMsg {// errCode 0: 校时成功且偏差在30ms以内;1: 校时成功但偏差可能在30ms以上;-1: 校时失败if (errCode == 0) {// 调用TXLivebase的getNetworkTimestamp即可获取NTP时间戳NSInteger ntpTime = [TXLiveBase getNetworkTimestamp];} else {// 重新调用updateNetworkTime启动一次校时[TXLiveBase updateNetworkTime];}}
NSDictionary *json = @{@"cmd": @"startChorus",// 约定时间合唱@"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;// 合唱命令if ([cmd isEqualToString:@"startChorus"]) {// 合唱开始时间NSObject *startPlayMusicTsObj = [json objectForKey:@"startPlayMusicTS"];NSString *musicId = [json objectForKey:@"musicId"];NSInteger startPlayMusicTs = ((NSNumber *)startPlayMusicTsObj).longLongValue;// 合唱约定时间和当前时间差值NSInteger startDelayMS = labs(startPlayMusicTs - [TXLiveBase getNetworkTimestamp]);// 开启预加载,根据合唱约定时间和当前ntp差值,跳跃歌曲进度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];// 播放歌曲TXAudioMusicParam *param = [[TXAudioMusicParam alloc] init];param.ID = musicId;param.path = url;param.loopCount = 0;param.publish = NO;[[subCloud getAudioEffectManager] startPlayMusic:param onStart:^(NSInteger errCode) {// 开始播放回调} onProgress:^(NSInteger progressMs, NSInteger durationMs) {// 歌曲进度回调} onComplete:^(NSInteger errCode) {// 播放完成回调}];}}
self.startPlayChorusMusicTs; //最初约定的合唱时间// 当前播放进度NSInteger currentProgress = [[self audioEffecManager] getMusicCurrentPosInMS:self.currentPlayMusicID];// 当前歌曲的理想播放时间进度NSInteger estimatedProgress = [TXLiveBase getNetworkTimestamp] - self.startPlayChorusMusicTs;if (estimatedProgress >= 0 && labs(currentProgress - estimatedProgress) > 50) {// 当播放进度超过50ms,进行修正[[subCloud getAudioEffectManager] seekMusicToPosInMS:self.currentPlayMusicID pts:estimatedProgress];}
文档反馈