// // RTCManager.m // YUMI // // Created by zu on 2021/10/19. // #import "RtcManager.h" #import "RtcImplDelegate.h" #import "TRTCRtcImpl.h" UIKIT_EXTERN NSString * kRoomBackMusicAudioMixingVolumeKey; UIKIT_EXTERN NSString * kRoomBackMusicCaptureVolumeKey; @interface RtcManager() @property (nonatomic, strong) id engine; @property (nonatomic, weak) id engineDelegate; @property (nonatomic, assign) RtcEngineType engineType; /** * 当前 Rtc 所在的房间 roomUid 。 */ @property(nonatomic, strong) NSString * enterdRoomUid; @end @implementation RtcManager + (instancetype)instance { static dispatch_once_t onceToken; static RtcManager *instance = nil; dispatch_once(&onceToken,^{ instance = [[self alloc] init]; }); return instance; } - (instancetype)init { self = [super init]; if (self) { _localMuted = NO; _remoteMuted = NO; _engineType = RtcEngineType_TRTC; } return self; } + (instancetype)initEngineWithType:(RtcEngineType)type delegate:(id _Nullable)delegate { RtcManager* rtcManager = [self instance]; [rtcManager setEngineType:type]; [rtcManager setEngineDelegate:delegate]; return rtcManager; } - (BOOL)enterRoom:(NSString *)roomUid { if (self.enterdRoomUid && [self.enterdRoomUid isEqualToString:roomUid]) { return YES; } return [self.engine joinChannel:roomUid completion:^{ [self muteRemote:NO]; [self muteLocal:NO]; self.enterdRoomUid = roomUid; }]; } - (BOOL)enterRoom:(NSString *)roomUid trtcSign:(nonnull NSString *)sign { if (self.enterdRoomUid && [self.enterdRoomUid isEqualToString:roomUid]) { return YES; } return [self.engine joinChannel:roomUid sign:sign completion:^{ [self muteRemote:NO]; [self muteLocal:NO]; self.enterdRoomUid = roomUid; }]; } - (BOOL)muteRemote:(BOOL)mute { return [self.engine muteRemote:mute]; } - (void)broadcast:(BOOL)on { [self.engine broadcast:on]; if (on) { [self.engine muteLocal:self.isLocalMuted]; } } - (BOOL)muteLocal:(BOOL)mute { return [self.engine muteLocal:mute]; } - (void)exitRoom { @kWeakify(self); [self.engine exitChannel:^{ @kStrongify(self); if (self) { [self muteRemote:NO]; [self muteLocal:NO]; self.enterdRoomUid = nil; } }]; } ///发起跨房通话 - (void)connectOtherRoom:(NSString *)roomUid userId:(NSString *)uid { [self.engine broadcast:YES]; [self.engine connectOtherRoom:roomUid userId:uid]; } ///退出跨房通话 - (void)disconnectOtherRoom { [self.engine disconnectOtherRoom]; } /// 静音某个人 /// @param userId 用户id - (void)muteOne:(BOOL)mute userId:(NSString *)userId { [self.engine muteRemote:mute userId:userId]; } - (void)destory { [self.engine destory]; } /// 播放背景音乐 /// @param filePath 音频文件的地址 /// @param musicId TRTC自己要的 - (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId completion:(void (^)(NSString *))completion{ return [self.engine playBackMusic:filePath musicId:musicId completion:completion]; } /// 改变播放器的状态 /// @param state 播放状态 - (BOOL)changePlayState:(BackMusicPlayState)state { return [self.engine changePlayState:state]; } ///背景音乐的音量大小 - (void)updateMusicSound:(int)soundVol { [[NSUserDefaults standardUserDefaults] setInteger:soundVol forKey:kRoomBackMusicAudioMixingVolumeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; [self.engine updateMusicSound:soundVol]; } ///背景人声的音量大小 - (void)updateUserSound:(int)soundVol { [[NSUserDefaults standardUserDefaults] setInteger:soundVol forKey:kRoomBackMusicCaptureVolumeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; [self.engine updateUserSound:soundVol]; } - (void)updateUserSoundWithUserInMic:(int)soundVol { [self.engine updateUserSound:soundVol]; } - (NSInteger)loadUserSound { // 查询 recEngine 的人声音量,如果没有数据,则设置为默认值 50 if ([self.engine loadUserSound] == 0) { [self updateUserSound:50]; return [[NSUserDefaults standardUserDefaults] integerForKey:kRoomBackMusicCaptureVolumeKey]; } return [self.engine loadUserSound]; } - (void)usersSpeaking:(NSMutableArray *)uids { if (self.engineDelegate) { [self.engineDelegate usersSpeaking:uids]; } } - (void)setRemoteMuted:(BOOL)remoteMuted { if ([self.engine muteRemote:remoteMuted]) { _remoteMuted = remoteMuted; } } - (void)setLocalMuted:(BOOL)localMuted { if ([self.engine muteLocal:localMuted]) { _localMuted = localMuted; } } - (void)setEngineType:(RtcEngineType)type { if (_engine && type != _engineType) { [_engine exitChannel:nil]; [_engine destory]; _engine = nil; _enterdRoomUid = nil; } _engineType = type; } - (void)setEngineDelegate:(id)delegate { _engineDelegate = delegate; } - (void)updateDelegate:(id _Nullable)delegate { _engineDelegate = delegate; } - (id)engine { if (!_engine) { _engine = [[TRTCRtcImpl alloc] initWithDelegate:self]; } return _engine; } @end