TXLiveRoomViewController.m 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. //
  2. // TXLiveRoomViewController.m
  3. // StudentDaya
  4. //
  5. // Created by 王智 on 2023/2/21.
  6. // Copyright © 2023 DayaMusic. All rights reserved.
  7. //
  8. #import "TXLiveRoomViewController.h"
  9. #import "TXLiteAVSDK_Professional/TXLiteAVSDK.h"
  10. #import "KSLiveEmptyView.h"
  11. #import "TXLiveVideoView.h"
  12. #import "LiveRoomHeadView.h"
  13. #import "LiveRoomBottomView.h"
  14. #import <Lottie/Lottie.h>
  15. #import "KSChatInputBarControl.h"
  16. #import "TXLiveChatListCell.h"
  17. #import "AppDelegate.h"
  18. #import <KSToolLibrary/UIDevice+TFDevice.h>
  19. #import "LiveRoomLikeLayer.h"
  20. #import "LiveAlertView.h"
  21. #import "KSShopCardView.h"
  22. #import "KSLiveWebViewController.h"
  23. #import "KSLiveBadNetView.h"
  24. #import "KSLiveEndView.h"
  25. #import "LiveAnimationView.h"
  26. #import "LiveApplyView.h"
  27. #import "LiveApplyingView.h"
  28. #import <ImSDK_Plus/ImSDK_Plus.h>
  29. #import "TXIMLinsenter.h"
  30. #import "TXLiveMessageModel.h"
  31. #import "TXLiveURLUtils.h"
  32. #import "TXLiveMessageCenter.h"
  33. #import "KSDragWindowManager.h"
  34. #import "TXLiveLoadingView.h"
  35. #import "TXUISeatMember.h"
  36. #import "TXSeatContainerView.h"
  37. #import "LiveRoomConfirmAlert.h"
  38. #import "LiveModuleService.h"
  39. #define AUTOHIDE_TIME (10.0f)
  40. typedef NS_ENUM(NSInteger, MICSTATUS) {
  41. MICSTATUS_NOMAL, // 正常状态
  42. MICSTATUS_WAITING, // 申请中
  43. MICSTATUS_CONNECTING, // 连麦中
  44. };
  45. @interface TXLiveRoomViewController ()<UIGestureRecognizerDelegate,KSChatInputBarControlDelegate,UITableViewDataSource,UITableViewDelegate,V2TXLivePlayerObserver,TRTCCloudDelegate>
  46. // 是否是观众
  47. @property (nonatomic, assign) BOOL isAudience;
  48. // rtc 连麦时使用
  49. @property (nonatomic, strong) TRTCCloud *trtcCloud;
  50. @property (nonatomic, strong) V2TXLivePlayer *livePlayer;
  51. @property (nonatomic, strong) LiveRoomHeadView *headView;
  52. @property (nonatomic, strong) LiveRoomBottomView *bottomView;
  53. // 视频主窗口容器
  54. @property (nonatomic, strong) UIView *videoContainerView;
  55. // 主屏videoView
  56. @property (nonatomic, strong) UIView *videoView;
  57. // 画面渲染的画面
  58. @property (nonatomic, strong) TXLiveVideoView *videoRenderView;
  59. // 切换按钮
  60. @property (nonatomic, strong) UIButton *switchButton;
  61. @property (nonatomic, strong) LOTAnimationView *animationView;
  62. /// 连麦的视图
  63. @property (nonatomic, strong) TXSeatContainerView *seatContainer;
  64. // 聊天UI
  65. /*!
  66. 消息列表CollectionView和输入框都在这个view里
  67. */
  68. @property(nonatomic, strong) UIView *messageContentView;
  69. /*!
  70. 会话页面的TableView
  71. */
  72. @property (nonatomic, strong) UITableView *conversationMessageTableView;
  73. /*!
  74. 聊天内容的消息Cell数据模型的数据源
  75. @discussion 数据源中存放的元素为消息Cell的数据模型,即RCDLiveMessageModel对象。
  76. */
  77. @property(nonatomic, strong) NSMutableArray<TXLiveMessageModel *> *conversationDataRepository;
  78. /**
  79. 输入工具栏
  80. */
  81. @property(nonatomic,strong) KSChatInputBarControl *inputBar;
  82. /**
  83. * 是否需要滚动到底部
  84. */
  85. @property(nonatomic, assign) BOOL isNeedScrollToButtom;
  86. /**
  87. 上次点赞按钮点击时间
  88. */
  89. @property(nonatomic, assign) NSTimeInterval lastClickPraiseTime;
  90. @property (nonatomic, assign) BOOL landScape;
  91. @property (nonatomic, assign) BOOL isHiddenHeadView;
  92. // 连麦状态
  93. @property (nonatomic, assign) MICSTATUS micStatus;
  94. @property (nonatomic, strong) LiveApplyView *applyView; // 未申请
  95. @property (nonatomic, strong) LiveApplyingView *applyingView; // 申请中
  96. @property (nonatomic, assign) BOOL isCreaterVideoEnable;
  97. @property (nonatomic, assign) BOOL isCloseRoom; // 是否直播结束
  98. /// 提示窗
  99. @property (nonatomic, strong) LiveAlertView *alertView;
  100. @property (nonatomic, strong) KSShopCardView *cardView;
  101. @property (nonatomic, assign) NSTimeInterval lastSendMsgTime;
  102. @property (nonatomic, strong) LiveAnimationView *enterAnimationView;
  103. @property (nonatomic, strong) KSLiveEndView *endView; // 直播结束显示的画面
  104. @property (nonatomic, strong) KSLiveEmptyView *emptyView;
  105. @property (nonatomic, strong) KSLiveBadNetView *netBadView;
  106. /// 是否在切换身份
  107. @property (nonatomic, assign) BOOL isTransferRole;
  108. @property (nonatomic, assign) BOOL hasSendWelcomeMessage;
  109. // 远端连麦用户
  110. @property (nonatomic, strong) NSMutableArray *remoteMemberArray;
  111. @property (nonatomic, assign) BOOL isPauseLive;
  112. @property (nonatomic, strong) TXLiveLoadingView *loadingView;
  113. @property (nonatomic, assign) BOOL hasShowSuspendView;
  114. @property (nonatomic, assign) BOOL muteRoomMic;
  115. @property (nonatomic, assign) BOOL micEnable;
  116. @property (nonatomic, strong) LiveRoomConfirmAlert *closeAlert;
  117. @property (nonatomic, assign) BOOL isConnectedFailed;
  118. #pragma mark --- 时间统计
  119. // 后端返回进入教室时间戳 ms
  120. @property (nonatomic, assign) NSTimeInterval enterTime;
  121. // 进入教室时间date
  122. @property (nonatomic, strong) NSDate *enterDate;
  123. @end
  124. // 用于记录点赞消息连续点击的次数
  125. static int clickPraiseBtnTimes = 0;
  126. @implementation TXLiveRoomViewController
  127. - (instancetype)init {
  128. self = [super init];
  129. if (self) {
  130. [self defaultConfig];
  131. [self default3AConfig];
  132. }
  133. return self;
  134. }
  135. - (void)default3AConfig {
  136. self.ANS = 0;
  137. self.AEC = 100;
  138. self.AGC = 0;
  139. }
  140. - (void)joinChatRoom {
  141. if (TXIM_LINSENTER.loginIMSuccess == NO) {
  142. [TXIM_LINSENTER TXIMLoginWithUserId:UserDefault(IM_USERID) sig:self.UserSig callback:^(BOOL isSuccess, NSString * _Nullable msg) {
  143. [self liveConversationJoin];
  144. }];
  145. }
  146. else {
  147. [self liveConversationJoin];
  148. }
  149. }
  150. - (void)liveConversationJoin {
  151. [[V2TIMManager sharedInstance] joinGroup:self.roomId msg:nil succ:^{
  152. dispatch_async(dispatch_get_main_queue(), ^{
  153. [self addConstMessage];
  154. [self sendWelcomeMessage];
  155. [self getGroupAttrInfo];
  156. [self getGroupCount];
  157. });
  158. } fail:^(int code, NSString *desc) {
  159. NSLog(@"");
  160. }];
  161. }
  162. - (void)getGroupAttrInfo {
  163. [[V2TIMManager sharedInstance] getGroupAttributes:self.roomId keys:nil succ:^(NSMutableDictionary<NSString *,NSString *> *groupAttributeList) {
  164. NSLog(@"");
  165. [self evaluateGroupInfo:groupAttributeList];
  166. } fail:^(int code, NSString *desc) {
  167. }];
  168. }
  169. - (void)getGroupCount {
  170. [[V2TIMManager sharedInstance] getGroupCounters:self.roomId keys:nil succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {
  171. NSLog(@"");
  172. [self evaluateGroupCount:groupCounters];
  173. } fail:^(int code, NSString *desc) {
  174. }];
  175. }
  176. - (void)addConstMessage {
  177. TXConstMessage *statement = [[TXConstMessage alloc] init];
  178. statement.userInfo = [self getCurrentUser];
  179. statement.text = @"欢迎进入直播课堂,请遵守相关法规,禁止传播低俗、暴力等不良信息。为孩子创造健康绿色的学习环境。";
  180. [self appendAndDisplayMessage:statement];
  181. }
  182. - (void)sendWelcomeMessage {
  183. TXLiveMessageWelcome *welcomeMessage = [[TXLiveMessageWelcome alloc] init];
  184. welcomeMessage.userInfo = [self getCurrentUser];
  185. MJWeakSelf;
  186. [self sendCustomMessage:welcomeMessage priority:V2TIM_PRIORITY_DEFAULT displayMessage:NO callback:^(BOOL success) {
  187. weakSelf.hasSendWelcomeMessage = YES;
  188. [weakSelf showEnterMessageAndJoinRoom];
  189. }];
  190. }
  191. - (void)showEnterMessageAndJoinRoom {
  192. NSString *contentString = [NSString stringWithFormat:@"%@ 进入直播间",UserDefault(NicknameKey)];
  193. [self showAnimationView:YES showMessag:contentString];
  194. }
  195. - (void)joinLiveRoom {
  196. [self changeEmptyStatusView];
  197. [self resetNetTips];
  198. [self startPlay];
  199. }
  200. - (void)viewWillAppear:(BOOL)animated {
  201. [super viewWillAppear:animated];
  202. [UIApplication sharedApplication].idleTimerDisabled = YES;
  203. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  204. [IQKeyboardManager sharedManager].enable = NO;
  205. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  206. // 重新渲染view
  207. KSDragWindowManager *manager = [KSDragWindowManager sharedManager];
  208. if (manager.hasShowWindow) {
  209. [manager resignDragWindow];
  210. }
  211. if (self.hasShowSuspendView) {
  212. [self.videoView addSubview:self.videoRenderView];
  213. [self.videoRenderView mas_updateConstraints:^(MASConstraintMaker *make) {
  214. make.left.right.bottom.top.mas_equalTo(self.videoView);
  215. }];
  216. _hasShowSuspendView = NO;
  217. }
  218. if (self.bottomView.isMute == NO) {
  219. [self muteVoiceAction];
  220. }
  221. }
  222. - (void)viewWillDisappear:(BOOL)animated {
  223. [super viewWillDisappear:animated];
  224. [UIApplication sharedApplication].idleTimerDisabled = NO;
  225. if (self.landScape) {
  226. [self changeOrientation:NO];
  227. }
  228. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  229. [IQKeyboardManager sharedManager].enable = YES;
  230. if (@available(iOS 13.0, *)) {
  231. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;
  232. } else {
  233. // Fallback on earlier versions
  234. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  235. }
  236. }
  237. - (void)defaultConfig {
  238. self.enableChat = YES;
  239. self.enableSeat = YES;
  240. self.enableLike = YES;
  241. self.needSwitchButton = NO;
  242. self.micStatus = MICSTATUS_NOMAL;
  243. self.isCreateInRoom = NO; // 默认主讲人不在房间
  244. self.blacklistFlag = NO;
  245. self.isCloseRoom = NO;
  246. self.lastClickPraiseTime = 0;
  247. self.isAudience = YES;
  248. self.isCreaterVideoEnable = YES;
  249. }
  250. - (void)viewDidLoad {
  251. [super viewDidLoad];
  252. // Do any additional setup after loading the view.
  253. self.ks_prefersNavigationBarHidden = YES;
  254. [self setupUI];
  255. [self registerNotification];
  256. [self joinChatRoom];
  257. [self joinLiveRoom];
  258. [self notiferJoinSuccessToServiceCallback:^{
  259. }];
  260. }
  261. - (void)registerNotification {
  262. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessageNotification:) name:OnReceiveTXLiveMessageNotification object:nil];
  263. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(liveroomLogOut) name:@"liveroomLogout" object:nil];
  264. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IMConnetedCallback) name:@"TXIMConnected" object:nil];
  265. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(muteAllAudio) name:@"muteLiveAudio" object:nil];
  266. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveGroupMessage:) name:OnReceiveTXLiveGroupNotification object:nil];
  267. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveGroupCountMessage:) name:OnReceiveTXLiveGroupCountNotification object:nil];
  268. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkTips) name:@"networkError" object:nil];
  269. }
  270. - (void)networkTips {
  271. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"直播已断开,请检查您的网络"];
  272. self.isConnectedFailed = YES;
  273. }
  274. - (void)IMConnetedCallback {
  275. self.isConnectedFailed = NO;
  276. dispatch_main_async_safe(^{
  277. // IM 连接成功回调
  278. if (self.hasSendWelcomeMessage) { // 如果已经发送了进入消息
  279. // 查询是否直播间开启
  280. [LiveModuleService imLiveBroadcastRoomQueryRoomRequest:KS_GET roomId:self.roomId success:^(NSDictionary * _Nonnull dic) {
  281. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  282. // 回调信息
  283. MJWeakSelf;
  284. [self notiferJoinSuccessToServiceCallback:^{
  285. [weakSelf sendWelcomeMessage];
  286. [weakSelf checkPlayerStatus];
  287. }];
  288. }
  289. else {
  290. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  291. [self quitRoom];
  292. }
  293. } faliure:^(NSError * _Nonnull error) {
  294. }];
  295. // 查询群属性
  296. [self getGroupAttrInfo];
  297. }
  298. });
  299. }
  300. - (void)checkPlayerStatus {
  301. if (self.isCloseRoom) {
  302. return;
  303. }
  304. if (self.isAudience) {
  305. if (self.livePlayer.isPlaying == NO) {
  306. [self resetNetTips];
  307. [self startPlay];
  308. }
  309. }
  310. else {
  311. [self connectHostWithStatus:NO];
  312. }
  313. }
  314. - (void)liveroomLogOut {
  315. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"该账号在其他设备上登录"];
  316. [self quitRoom];
  317. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  318. [[NSNotificationCenter defaultCenter] postNotificationName:@"backLoginView" object:nil];
  319. });
  320. }
  321. - (void)muteAllAudio {
  322. [self muteVoiceAction];
  323. }
  324. #pragma mark ----- // 观众上下麦
  325. - (void)connectHostWithStatus:(BOOL)isConnect {
  326. // 如果直播结束 不转换身份
  327. if (self.isCloseRoom == YES) {
  328. return;
  329. }
  330. // 转换中,不再处理转换身份
  331. if (self.isTransferRole) {
  332. return;
  333. }
  334. if (isConnect && self.isAudience == NO) {
  335. self.micStatus = MICSTATUS_CONNECTING;
  336. return;
  337. }
  338. else if (isConnect == NO && self.isAudience) {
  339. self.micStatus = MICSTATUS_NOMAL;
  340. return;
  341. }
  342. self.isTransferRole = YES;
  343. self.micEnable = YES;
  344. // 清理当前视图
  345. [self removeSeatContainer];
  346. self.remoteMemberArray = [NSMutableArray array];
  347. // 移除状态视图
  348. if (isConnect) {
  349. [self stopPlay];
  350. [self hideTipsView];
  351. if (self.isCreaterVideoEnable) {
  352. [self hideEmptyView];
  353. }
  354. self.isAudience = NO;
  355. [self enterLiveRTCRoom];
  356. self.isTransferRole = NO;
  357. self.micStatus = MICSTATUS_CONNECTING;
  358. }
  359. else {
  360. self.isAudience = YES;
  361. if (self.isPauseLive == NO) {
  362. [self startPlay];
  363. }
  364. if (_trtcCloud) {
  365. [self.trtcCloud stopAllRemoteView];
  366. [self.trtcCloud stopLocalAudio];
  367. [self.trtcCloud exitRoom];
  368. _trtcCloud = nil;
  369. [TRTCCloud destroySharedInstance];
  370. }
  371. self.micStatus = MICSTATUS_NOMAL;
  372. self.isTransferRole = NO;
  373. }
  374. [self muteVoiceAction];
  375. }
  376. - (void)setupUI {
  377. self.view.backgroundColor = HexRGB(0x25292e);
  378. [self.view addSubview:self.emptyView];
  379. [self.view addSubview:self.endView];
  380. [self.view addSubview:self.videoContainerView];
  381. [self.videoContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
  382. make.left.right.top.bottom.mas_equalTo(self.view);
  383. }];
  384. [self.videoContainerView addSubview:self.videoView];
  385. // 异常的view
  386. [self.videoContainerView addSubview:self.netBadView];
  387. // 按钮和视频的布局
  388. if (self.needSwitchButton) {
  389. [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
  390. make.left.right.mas_equalTo(self.videoContainerView);
  391. if (@available(iOS 11.0, *)) {
  392. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(160);
  393. } else {
  394. // Fallback on earlier versions
  395. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(160);
  396. }
  397. CGFloat rate = 9 / 16.0f;
  398. make.height.mas_equalTo(self.videoContainerView.mas_width).multipliedBy(rate);
  399. make.width.height.mas_equalTo(self.videoContainerView).priorityLow();
  400. make.width.height.lessThanOrEqualTo(self.videoContainerView);
  401. }];
  402. // 显示切换按钮
  403. [self.videoContainerView addSubview:self.switchButton];
  404. [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
  405. make.right.mas_equalTo(self.videoContainerView.mas_right).offset(-10);
  406. make.width.height.mas_equalTo(33);
  407. make.bottom.mas_equalTo(self.videoView.mas_bottom).offset(-10);
  408. }];
  409. self.switchButton.hidden = NO;
  410. }
  411. else {
  412. [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
  413. make.left.top.bottom.right.mas_equalTo(self.videoContainerView);
  414. }];
  415. }
  416. // 添加视频预览图
  417. [self.videoView addSubview:self.videoRenderView];
  418. [self.videoRenderView mas_makeConstraints:^(MASConstraintMaker *make) {
  419. make.left.right.top.bottom.mas_equalTo(self.videoView);
  420. }];
  421. [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
  422. make.left.right.top.bottom.mas_equalTo(self.videoView);
  423. }];
  424. self.emptyView.hidden = YES;
  425. // 直播结束的画面
  426. [self.endView mas_makeConstraints:^(MASConstraintMaker *make) {
  427. make.left.right.top.bottom.mas_equalTo(self.videoView);
  428. }];
  429. self.endView.hidden = YES;
  430. [self.netBadView mas_makeConstraints:^(MASConstraintMaker *make) {
  431. make.left.right.top.bottom.mas_equalTo(self.videoView);
  432. }];
  433. self.netBadView.hidden = YES;
  434. [self.videoContainerView addSubview:self.headView];
  435. self.headView.boardcastName.text = [NSString returnNoNullStringWithString:self.createrName];
  436. if (![NSString isEmptyString:self.createrAvatal]) {
  437. [self.headView.boardcastAvatal sd_setImageWithURL:[NSURL URLWithString:[self.createrAvatal getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:@"teacher_logo"]];
  438. }
  439. [self countLikeMessageCount];
  440. [self countMemberCount];
  441. [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
  442. make.left.right.mas_equalTo(self.videoContainerView);
  443. make.height.mas_equalTo(80);
  444. if (@available(iOS 11.0, *)) {
  445. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(0);
  446. } else {
  447. // Fallback on earlier versions
  448. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(0);
  449. }
  450. }];
  451. // 聊天区域
  452. [self.videoContainerView addSubview:self.messageContentView];
  453. if (self.hideCartButton == NO) {
  454. [self.videoContainerView addSubview:self.animationView];
  455. [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
  456. if (@available(iOS 11.0, *)) {
  457. make.bottom.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideBottom).offset(-9);
  458. } else {
  459. // Fallback on earlier versions
  460. make.bottom.mas_equalTo(self.videoContainerView.mas_bottom).offset(-9);
  461. }
  462. make.centerX.mas_equalTo(self.videoContainerView.mas_right).offset(-80);
  463. make.width.mas_equalTo(48);
  464. make.height.mas_equalTo(56);
  465. }];
  466. [self.animationView play];
  467. }
  468. [self.videoContainerView addSubview:self.bottomView];
  469. self.bottomView.hideCartVButton = self.hideCartButton;
  470. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  471. make.left.right.mas_equalTo(self.videoContainerView);
  472. if (@available(iOS 11.0, *)) {
  473. make.bottom.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideBottom).offset(-5);
  474. } else {
  475. // Fallback on earlier versions
  476. make.bottom.mas_equalTo(self.videoContainerView.mas_bottom).offset(-5);
  477. }
  478. make.height.mas_equalTo(44);
  479. }];
  480. // 聊天页面
  481. if (self.needSwitchButton) {
  482. [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
  483. make.left.mas_equalTo(self.videoContainerView.mas_left);
  484. make.right.mas_equalTo(self.videoContainerView.mas_right);
  485. make.bottom.mas_equalTo(self.bottomView.mas_top).offset(-5);
  486. make.top.mas_equalTo(self.videoView.mas_bottom).offset(10).priorityLow();
  487. }];
  488. }
  489. else {
  490. [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
  491. make.left.mas_equalTo(self.videoContainerView.mas_left);
  492. make.right.mas_equalTo(self.videoContainerView.mas_right);
  493. make.bottom.mas_equalTo(self.bottomView.mas_top).offset(-10);
  494. make.height.mas_equalTo(180);
  495. }];
  496. }
  497. [self.videoContainerView addSubview:self.inputBar];
  498. [_inputBar setBackgroundColor: [UIColor whiteColor]];
  499. [_inputBar setFrame:CGRectMake(0, KPortraitHeight, KPortraitWidth , 50)];
  500. [_inputBar setHidden:YES];
  501. [self.messageContentView addSubview:self.conversationMessageTableView];
  502. [self.conversationMessageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  503. make.left.top.mas_equalTo(self.messageContentView);
  504. make.width.mas_equalTo(KPortraitWidth);
  505. make.bottom.mas_equalTo(self.messageContentView.mas_bottom);
  506. }];
  507. UITapGestureRecognizer *resetBottomTapGesture =[[UITapGestureRecognizer alloc]
  508. initWithTarget:self
  509. action:@selector(resetBottomGesture:)];
  510. resetBottomTapGesture.delegate = self;
  511. [self.videoContainerView addGestureRecognizer:resetBottomTapGesture];
  512. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewAnimation:)];
  513. tapGesture.delegate = self;
  514. [self.videoView addGestureRecognizer:tapGesture];
  515. }
  516. - (void)headViewAnimation:(UIGestureRecognizer *)gestureRecognizer {
  517. if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
  518. [self setDefaultBottomViewStatus];
  519. }
  520. if (self.landScape) {
  521. self.isHiddenHeadView = !self.isHiddenHeadView;
  522. }
  523. }
  524. #pragma mark -- 加入直播间和退出直播间回到服务
  525. - (void)notiferJoinSuccessToServiceCallback:(void(^)(void))callback {
  526. [LiveModuleService LiveroomJoinRequest:KS_GET roomId:self.roomId success:^(NSDictionary * _Nonnull dic) {
  527. NSTimeInterval enterTime = [dic ks_integerValueForKey:@"data"];
  528. self.enterTime = enterTime;
  529. NSDate *currentDate = [NSDate date];
  530. self.enterDate = currentDate;
  531. callback();
  532. } faliure:^(NSError * _Nonnull error) {
  533. callback();
  534. }];
  535. }
  536. - (void)quitNotiferService {
  537. NSDate *currentDate = [NSDate date];
  538. NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.enterDate];
  539. NSInteger time = (NSInteger)(timeInterval *1000);
  540. if (self.enterTime > 0) {
  541. time = self.enterTime + time;
  542. }
  543. else {
  544. time = (NSInteger)([currentDate timeIntervalSince1970] *1000);
  545. }
  546. [LiveModuleService LiveroomQuit:KS_POST time:time success:^(NSDictionary * _Nonnull dic) {
  547. if ([dic ks_integerValueForKey:@"errCode"] == 0) {
  548. NSLog(@"success");
  549. }
  550. else {
  551. NSLog(@"----- error %@", [dic ks_stringValueForKey:@"errMsg"]);
  552. }
  553. } faliure:^(NSError * _Nonnull error) {
  554. }];
  555. }
  556. #pragma mark ------ 播放控制
  557. - (void)startPlay {
  558. if ([self.livePlayer isPlaying] == NO) {
  559. [self resetNetTips];
  560. if (self.isCreaterVideoEnable) {
  561. [self hideEmptyView];
  562. }
  563. [self showLoading];
  564. self.videoRenderView.hasMainStream = YES;
  565. self.videoRenderView.hasSubStream = NO;
  566. [self.livePlayer setRenderView:self.videoRenderView.mainView];
  567. if (self.needSwitchButton) {
  568. [self.livePlayer setRenderFillMode:V2TXLiveFillModeFit];
  569. }
  570. else {
  571. [self.livePlayer setRenderFillMode:V2TXLiveFillModeFill];
  572. }
  573. // 获取url的方法
  574. NSString *streamId = [NSString stringWithFormat:@"%@_%@", self.roomId, self.createrId];
  575. NSString *url = [TXLiveURLUtils generateRtmpPlayUrl:streamId playDomain:self.playDomain liveUrlKey:self.liveUrlKey];
  576. [self.livePlayer startLivePlay:url];
  577. [self muteVoiceAction];
  578. }
  579. }
  580. - (void)stopPlay {
  581. if (_livePlayer) {
  582. if ([self.livePlayer isPlaying]) {
  583. [self hideLoading];
  584. [self.livePlayer stopPlay];
  585. }
  586. }
  587. }
  588. // 不播放声音
  589. - (void)pauserAudio {
  590. [self.livePlayer pauseAudio];
  591. }
  592. // 恢复声音
  593. - (void)resumeAudio {
  594. [self.livePlayer resumeAudio];
  595. }
  596. #pragma mark ---- V2TXLivePlayerObserver
  597. - (void)onError:(id<V2TXLivePlayer>)player code:(V2TXLiveCode)code message:(NSString *)msg extraInfo:(NSDictionary *)extraInfo {
  598. if (code == V2TXLIVE_ERROR_DISCONNECTED) {
  599. // 连接断开
  600. if (self.isPauseLive == NO && self.isCreateInRoom) {
  601. [self hideLoading];
  602. [self showBadNetTips];
  603. }
  604. }
  605. else if (code == V2TXLIVE_WARNING_VIDEO_BLOCK) {
  606. }
  607. NSLog(@"---- V2TXLivePlayer %zd message:%@", code, msg);
  608. }
  609. - (void)onConnected:(id<V2TXLivePlayer>)player extraInfo:(NSDictionary *)extraInfo {
  610. NSLog(@" ------ connected %@", extraInfo);
  611. self.isPauseLive = NO;
  612. self.isCreateInRoom = YES;
  613. if (self.isCreaterVideoEnable) {
  614. [self hideEmptyView];
  615. }
  616. }
  617. - (void)onVideoPlaying:(id<V2TXLivePlayer>)player firstPlay:(BOOL)firstPlay extraInfo:(NSDictionary *)extraInfo {
  618. NSLog(@" ------ onVideoPlaying %@", extraInfo);
  619. [self hideLoading];
  620. }
  621. - (void)onVideoResolutionChanged:(id<V2TXLivePlayer>)player width:(NSInteger)width height:(NSInteger)height {
  622. NSLog(@" ------ onVideoResolutionChanged %zd %zd", width, height);
  623. }
  624. // 直播状态
  625. - (void)onStatisticsUpdate:(id<V2TXLivePlayer>)player statistics:(V2TXLivePlayerStatistics *)statistics {
  626. NSLog(@"----- V2TXLivePlayerStatistics desc %@", statistics.description);
  627. }
  628. - (void)onReceiveSeiMessage:(id<V2TXLivePlayer>)player payloadType:(int)payloadType data:(NSData *)data {
  629. NSLog(@"----- onReceiveSeiMessage desc %@", [data mj_JSONObject]);
  630. if (self.isAudience == YES && self.isPauseLive == NO) {
  631. NSDictionary *parm = [data mj_JSONObject];
  632. if ([[parm ks_stringValueForKey:@"api"] isEqualToString:@"sync"]) { // 同步连麦人数
  633. NSArray *memberArray = [parm ks_arrayValueForKey:@"seatUser"];
  634. NSMutableArray *remoteSeatArray = [NSMutableArray arrayWithArray:memberArray];
  635. // 移除自己
  636. NSMutableArray *seatArray = [NSMutableArray array];
  637. for (NSDictionary *userParm in remoteSeatArray) {
  638. if (![[userParm ks_stringValueForKey:@"userId"] isEqualToString:UserDefault(IM_USERID)]) {
  639. TXUISeatMember *member = [[TXUISeatMember alloc] init];
  640. member.userId = [userParm ks_stringValueForKey:@"userId"];
  641. member.muteMic = [userParm ks_boolValueForKey:@"micStatus"];
  642. [seatArray addObject:member];
  643. }
  644. }
  645. [self refreshSeatWithMember:seatArray];
  646. // if (self.remoteMemberArray.count != seatArray.count) { // 如果连麦人数不同
  647. // [self refreshSeatWithMember:seatArray];
  648. // }
  649. // else {
  650. // BOOL isSameUser = YES;
  651. // NSMutableArray *seatMemberArr = [self.remoteMemberArray mutableCopy];
  652. //
  653. // for (TXUISeatMember *member in seatMemberArr) {
  654. //
  655. // if (![remoteSeatArray containsObject:member]) {
  656. // isSameUser = NO;
  657. // }
  658. // }
  659. // if (isSameUser == NO) {
  660. // [self refreshSeatWithMember:seatArray];
  661. // }
  662. // }
  663. }
  664. }
  665. else {
  666. [self refreshSeatWithMember:@[]];
  667. }
  668. }
  669. - (void)refreshSeatWithMember:(NSArray *)memberArray {
  670. self.remoteMemberArray = [NSMutableArray arrayWithArray:memberArray];
  671. [self renderSeatView];
  672. }
  673. #pragma mark --- trtcCloud delegate
  674. // 远端主播加入房间
  675. - (void)onRemoteUserEnterRoom:(NSString *)userId {
  676. if (![self judgeContainerRemoteUser:userId] && ![userId isEqualToString:self.createrId]) { // 如果不包含当前用户 添加 和刷新连麦
  677. TXUISeatMember *member = [[TXUISeatMember alloc] init];
  678. member.userId = userId;
  679. member.muteMic = YES;
  680. [self.remoteMemberArray addObject:member];
  681. }
  682. [self renderSeatView];
  683. }
  684. // 远端主播离开
  685. - (void)onRemoteUserLeaveRoom:(NSString *)userId reason:(NSInteger)reason {
  686. if ([self judgeContainerRemoteUser:userId]) { // 如果包含当前用户 移除当前用和刷新连麦
  687. [self removeMemberUserId:userId];
  688. }
  689. [self renderSeatView];
  690. }
  691. - (BOOL)judgeContainerRemoteUser:(NSString *)userId {
  692. BOOL containUser = NO;
  693. NSMutableArray *remoteMemArray = [self.remoteMemberArray mutableCopy];
  694. for (TXUISeatMember *member in remoteMemArray) {
  695. if ([member.userId isEqualToString:userId]) {
  696. containUser = YES;
  697. }
  698. }
  699. return containUser;
  700. }
  701. - (void)removeMemberUserId:(NSString *)userId {
  702. NSMutableArray *remoteMemArray = [self.remoteMemberArray mutableCopy];
  703. for (TXUISeatMember *member in remoteMemArray) {
  704. if ([member.userId isEqualToString:userId]) {
  705. [self.remoteMemberArray removeObject:member];
  706. return;
  707. }
  708. }
  709. }
  710. - (void)onWarning:(TXLiteAVWarning)warningCode warningMsg:(NSString *)warningMsg extInfo:(NSDictionary *)extInfo {
  711. NSLog(@" warningMsg %@ extInfo:%@", warningMsg, extInfo.description);
  712. }
  713. // 远端视频
  714. - (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
  715. NSLog(@"creater remote videoStream status: %d", available);
  716. if (available) {
  717. self.videoRenderView.hasMainStream = YES;
  718. if (self.videoRenderView.hasSubStream) {
  719. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeBig view:self.videoRenderView.smallView];
  720. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeSub view:self.videoRenderView.mainView];
  721. }
  722. else {
  723. [self.trtcCloud stopRemoteView:self.createrId streamType:TRTCVideoStreamTypeSub];
  724. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeBig view:self.videoRenderView.mainView];
  725. }
  726. }
  727. else {
  728. self.videoRenderView.hasMainStream = NO;
  729. [self.trtcCloud stopRemoteView:self.createrId streamType:TRTCVideoStreamTypeBig];
  730. }
  731. }
  732. - (void)onUserSubStreamAvailable:(NSString *)userId available:(BOOL)available {
  733. NSLog(@"creater remote subStream status: %d", available);
  734. if (available) {
  735. self.videoRenderView.hasSubStream = YES;
  736. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeSub view:self.videoRenderView.mainView];
  737. if (self.videoRenderView.hasMainStream) {
  738. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeBig view:self.videoRenderView.smallView];
  739. }
  740. }
  741. else {
  742. self.videoRenderView.hasSubStream = NO;
  743. [self.trtcCloud stopRemoteView:self.createrId streamType:TRTCVideoStreamTypeSub];
  744. if (self.videoRenderView.hasMainStream) {
  745. [self.trtcCloud startRemoteView:self.createrId streamType:TRTCVideoStreamTypeBig view:self.videoRenderView.mainView];
  746. }
  747. }
  748. }
  749. - (void)onFirstVideoFrame:(NSString *)userId streamType:(TRTCVideoStreamType)streamType width:(int)width height:(int)height {
  750. }
  751. - (void)onRecvSEIMsg:(NSString *)userId message:(NSData *)message {
  752. // NSDictionary *parm = [message mj_JSONObject];
  753. }
  754. - (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
  755. for (TXUISeatMember *member in self.remoteMemberArray) {
  756. if ([member.userId isEqualToString:userId]) {
  757. member.muteMic = !available;
  758. }
  759. }
  760. [self renderSeatView];
  761. }
  762. #pragma mark - gesture and button action
  763. - (void)resetBottomGesture:(UIGestureRecognizer *)gestureRecognizer {
  764. if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
  765. [self setDefaultBottomViewStatus];
  766. }
  767. }
  768. - (void)setDefaultBottomViewStatus {
  769. [self.inputBar setInputBarStatus:KSBottomBarStatusDefault];
  770. [self.inputBar setHidden:YES];
  771. }
  772. #pragma mark --- setter
  773. - (void)setIsHiddenHeadView:(BOOL)isHiddenHeadView {
  774. if (_isHiddenHeadView == isHiddenHeadView) {
  775. return;
  776. }
  777. _isHiddenHeadView = isHiddenHeadView;
  778. //告知需要更改约束
  779. [self.view setNeedsUpdateConstraints];
  780. [UIView animateWithDuration:0.5 animations:^{
  781. if (isHiddenHeadView) {
  782. [self.headView mas_updateConstraints:^(MASConstraintMaker *make) {
  783. if (@available(iOS 11.0, *)) {
  784. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(-80);
  785. } else {
  786. // Fallback on earlier versions
  787. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(-80);
  788. }
  789. }];
  790. }
  791. else {
  792. [self.headView mas_updateConstraints:^(MASConstraintMaker *make) {
  793. if (@available(iOS 11.0, *)) {
  794. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(10);
  795. } else {
  796. // Fallback on earlier versions
  797. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(10);
  798. }
  799. }];
  800. }
  801. [self.headView.superview layoutIfNeeded];
  802. }];
  803. }
  804. #pragma mark ----- landscape action
  805. - (void)transferToLandscape {
  806. [self setDefaultBottomViewStatus];
  807. [self changeOrientation:!self.landScape];
  808. }
  809. - (void)changeOrientation:(BOOL)isLandScape {
  810. self.landScape = isLandScape;
  811. if (isLandScape) {
  812. if (IS_IPAD) {
  813. self.zh_statusBarHidden = YES;
  814. }
  815. // 切换到横屏
  816. AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  817. delegate.allowAutoRotate = YES;
  818. [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight inController:self];
  819. }
  820. else {
  821. if (IS_IPAD) {
  822. self.zh_statusBarHidden = NO;
  823. }
  824. AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  825. delegate.allowAutoRotate = NO;
  826. [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait inController:self];
  827. }
  828. self.isHiddenHeadView = NO;
  829. [self updateConstraint];
  830. }
  831. - (void)updateConstraint {
  832. if (self.landScape) {
  833. self.bottomView.hideChatView = YES;
  834. self.messageContentView.hidden = YES;
  835. self.headView.hideQuitButton = YES;
  836. [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
  837. if (IS_IPAD) {
  838. make.left.right.mas_equalTo(self.videoContainerView);
  839. make.centerX.mas_equalTo(self.videoContainerView.mas_centerX);
  840. CGFloat rate = 9 / 16.0f;
  841. make.height.mas_equalTo(self.videoContainerView.mas_width).multipliedBy(rate);
  842. make.width.height.mas_equalTo(self.videoContainerView).priorityLow();
  843. make.width.height.lessThanOrEqualTo(self.videoContainerView);
  844. }
  845. else {
  846. make.top.bottom.mas_equalTo(self.videoContainerView);
  847. make.centerX.mas_equalTo(self.videoContainerView.mas_centerX);
  848. // 宽高设置
  849. CGFloat rate = 16 / 9.0f;
  850. make.width.mas_equalTo(self.videoContainerView.mas_height).multipliedBy(rate);
  851. make.width.height.mas_equalTo(self.videoContainerView).priorityLow();
  852. make.width.height.lessThanOrEqualTo(self.videoContainerView);
  853. }
  854. }];
  855. [self.switchButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  856. make.right.mas_equalTo(self.videoContainerView.mas_right).offset(-10);
  857. make.width.height.mas_equalTo(33);
  858. make.centerY.mas_equalTo(self.videoView.mas_centerY);
  859. }];
  860. }
  861. else {
  862. self.bottomView.hideChatView = NO;
  863. self.messageContentView.hidden = NO;
  864. self.headView.hideQuitButton = NO;
  865. [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
  866. make.left.right.mas_equalTo(self.videoContainerView);
  867. if (@available(iOS 11.0, *)) {
  868. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(160);
  869. } else {
  870. // Fallback on earlier versions
  871. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(160);
  872. }
  873. CGFloat rate = 9 / 16.0f;
  874. make.height.mas_equalTo(self.videoContainerView.mas_width).multipliedBy(rate);
  875. make.width.height.mas_equalTo(self.videoContainerView).priorityLow();
  876. make.width.height.lessThanOrEqualTo(self.videoContainerView);
  877. }];
  878. [self.switchButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  879. make.right.mas_equalTo(self.videoContainerView.mas_right).offset(-10);
  880. make.width.height.mas_equalTo(33);
  881. make.bottom.mas_equalTo(self.videoView.mas_bottom).offset(-10);
  882. }];
  883. }
  884. // 更新连麦UI
  885. if ([self.videoContainerView.subviews containsObject:self.seatContainer]) {
  886. [self updateSeatContainerConstraint];
  887. }
  888. }
  889. - (void)showEmptyViewDisplay:(BOOL)isPublish {
  890. NSString *imageName = @"";
  891. NSString *descMessage = @"";
  892. if (isPublish) {
  893. imageName = @"liveVideo_close";
  894. descMessage = @"主讲人已关闭画面!";
  895. }
  896. else {
  897. imageName = @"liveroom_empty";
  898. descMessage = @"休息一下,马上回来!";
  899. }
  900. [self.emptyView configViewWithImageName:imageName desc:descMessage];
  901. [self hideTipsView];
  902. self.videoView.hidden = YES;
  903. self.emptyView.hidden = NO;
  904. }
  905. - (void)hideEmptyView {
  906. self.emptyView.hidden = YES;
  907. self.videoView.hidden = NO;
  908. }
  909. - (void)changeEmptyStatusView {
  910. if (self.isCloseRoom == NO) { // 只有在非结束直播状态下
  911. if (self.isPauseLive) { // 暂停直播
  912. [self showEmptyViewDisplay:NO];
  913. }
  914. else { // 如果未暂停直播
  915. if (self.isCreaterVideoEnable) { // 如果摄像头开启
  916. [self hideTipsView];
  917. [self hideEmptyView];
  918. }
  919. else { // 主播关闭了摄像头
  920. [self showEmptyViewDisplay:YES];
  921. }
  922. }
  923. }
  924. }
  925. - (void)renderSeatView {
  926. NSMutableArray *seatArray = [NSMutableArray array];
  927. NSArray *remoteUserArray = [self.remoteMemberArray mutableCopy];
  928. for (TXUISeatMember *member in remoteUserArray) {
  929. [seatArray addObject:member];
  930. }
  931. BOOL isContainSelf = NO;
  932. for (TXUISeatMember *member in seatArray) {
  933. if ([member.userId isEqualToString:UserDefault(IM_USERID)]) {
  934. isContainSelf = YES;
  935. break;
  936. }
  937. }
  938. if (self.micStatus == MICSTATUS_CONNECTING && !isContainSelf) {
  939. TXUISeatMember *currentMember = [[TXUISeatMember alloc] init];
  940. currentMember.userId = UserDefault(IM_USERID);
  941. currentMember.muteMic = !self.micEnable;
  942. [seatArray addObject:currentMember];
  943. }
  944. NSLog(@"-- seat member--- %zd -", seatArray.count);
  945. if (seatArray.count) {
  946. if (![self.videoContainerView.subviews containsObject:self.seatContainer]) {
  947. [self.videoContainerView addSubview:self.seatContainer];
  948. }
  949. [self updateSeatContainerConstraint];
  950. // self.seatContainer.seatMemberArray = [NSMutableArray arrayWithArray:seatArray];
  951. // [self.seatContainer refreshSeatUI];
  952. [self.seatContainer refreshSeatUIWithMember:[NSMutableArray arrayWithArray:seatArray]];
  953. }
  954. else {
  955. [self removeSeatContainer];
  956. }
  957. }
  958. // 移除连麦视图
  959. - (void)removeSeatContainer {
  960. if ([self.videoContainerView.subviews containsObject:self.seatContainer]) {
  961. [self.seatContainer removeFromSuperview];
  962. }
  963. }
  964. - (void)updateSeatContainerConstraint {
  965. if (self.landScape) {
  966. [self.seatContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
  967. if (@available(iOS 11.0, *)) {
  968. make.left.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideLeft);
  969. } else {
  970. // Fallback on earlier versions
  971. make.left.mas_equalTo(self.videoContainerView.mas_left);
  972. }
  973. make.right.mas_equalTo(self.videoContainerView.mas_right).offset(-70);
  974. if (@available(iOS 11.0, *)) {
  975. make.top.mas_equalTo(self.videoContainerView.mas_safeAreaLayoutGuideTop).offset(85);
  976. } else {
  977. // Fallback on earlier versions
  978. make.top.mas_equalTo(self.videoContainerView.mas_top).offset(85);
  979. }
  980. make.height.mas_equalTo(70);
  981. }];
  982. }
  983. else {
  984. [self.seatContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
  985. make.left.right.mas_equalTo(self.videoContainerView);
  986. make.top.mas_equalTo(self.headView.mas_bottom).offset(0);
  987. make.height.mas_equalTo(70);
  988. }];
  989. }
  990. }
  991. #pragma mark ----- 加入TRC房间
  992. - (void)enterLiveRTCRoom {
  993. self.videoRenderView.hasMainStream = NO;
  994. self.videoRenderView.hasSubStream = NO;
  995. TRTCParams *parms = [[TRTCParams alloc] init];
  996. parms.sdkAppId = CONFIG_TXSDKAPPID;
  997. parms.userSig = self.UserSig;
  998. parms.role = TRTCRoleAnchor;
  999. parms.strRoomId = self.roomId;
  1000. parms.userId = UserDefault(IM_USERID);
  1001. [self.trtcCloud addDelegate:self];
  1002. [self config3AParams];
  1003. [self configVideoAndAudioSetting];
  1004. [self.trtcCloud enterRoom:parms appScene:TRTCAppSceneLIVE];
  1005. TRTCRenderParams *rendParams = [[TRTCRenderParams alloc] init];
  1006. if (self.needSwitchButton) {
  1007. rendParams.fillMode = TRTCVideoFillMode_Fit;
  1008. }
  1009. else {
  1010. rendParams.fillMode = TRTCVideoFillMode_Fill;
  1011. }
  1012. [self.trtcCloud setRemoteRenderParams:self.createrId streamType:TRTCVideoStreamTypeBig params:rendParams];
  1013. [self.trtcCloud setRemoteRenderParams:self.createrId streamType:TRTCVideoStreamTypeSub params:rendParams];
  1014. }
  1015. - (void)configVideoAndAudioSetting {
  1016. [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
  1017. TRTCVideoEncParam *videoEncParam = [[TRTCVideoEncParam alloc] init];
  1018. videoEncParam.videoFps = 24;
  1019. videoEncParam.resMode = TRTCVideoResolutionModePortrait;
  1020. videoEncParam.videoResolution = TRTCVideoResolution_960_540;
  1021. [self.trtcCloud setVideoEncoderParam:videoEncParam];
  1022. if (self.muteRoomMic) {
  1023. [self micButtonAction:self.muteRoomMic];
  1024. }
  1025. }
  1026. // 配置3A参数
  1027. - (void)config3AParams {
  1028. [self configANSParm:self.ANS];
  1029. [self configAECParm:self.AEC];
  1030. [self configAGCParm:self.AGC];
  1031. }
  1032. // 背景音抑制
  1033. - (void)configANSParm:(NSInteger)number {
  1034. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1035. [dic setValue:@"enableAudioANS" forKey:@"api"];
  1036. NSMutableDictionary *subDic = [NSMutableDictionary dictionary];
  1037. if (number > 0) {
  1038. [subDic setValue:@(1) forKey:@"enable"];
  1039. [subDic setValue:@(number) forKey:@"level"];
  1040. }
  1041. else {
  1042. [subDic setValue:@(0) forKey:@"enable"];
  1043. [subDic setValue:@(0) forKey:@"level"];
  1044. }
  1045. [dic setValue:subDic forKey:@"params"];
  1046. NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
  1047. NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1048. [self.trtcCloud callExperimentalAPI:jsonString];
  1049. }
  1050. // 回声消除
  1051. - (void)configAECParm:(NSInteger)number {
  1052. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1053. [dic setValue:@"enableAudioAEC" forKey:@"api"];
  1054. NSMutableDictionary *subDic = [NSMutableDictionary dictionary];
  1055. if (number > 0) {
  1056. [subDic setValue:@(1) forKey:@"enable"];
  1057. [subDic setValue:@(number) forKey:@"level"];
  1058. }
  1059. else {
  1060. [subDic setValue:@(0) forKey:@"enable"];
  1061. [subDic setValue:@(0) forKey:@"level"];
  1062. }
  1063. [dic setValue:subDic forKey:@"params"];
  1064. NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
  1065. NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1066. [self.trtcCloud callExperimentalAPI:jsonString];
  1067. }
  1068. // 自动增益
  1069. - (void)configAGCParm:(NSInteger)number {
  1070. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1071. [dic setValue:@"enableAudioAGC" forKey:@"api"];
  1072. NSMutableDictionary *subDic = [NSMutableDictionary dictionary];
  1073. if (number > 0) {
  1074. [subDic setValue:@(1) forKey:@"enable"];
  1075. [subDic setValue:@(number) forKey:@"level"];
  1076. }
  1077. else {
  1078. [subDic setValue:@(0) forKey:@"enable"];
  1079. [subDic setValue:@(0) forKey:@"level"];
  1080. }
  1081. [dic setValue:subDic forKey:@"params"];
  1082. NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
  1083. NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1084. [self.trtcCloud callExperimentalAPI:jsonString];
  1085. }
  1086. #pragma mark -------- live message handle
  1087. - (NSString *)getMsgClassName:(NSString *)messageObjectName {
  1088. TXLiveMessageModel *configModel = [[TXLiveMessageModel alloc] init];
  1089. NSString *className = [configModel getMessageClassName:messageObjectName];
  1090. return className;
  1091. }
  1092. /**
  1093. * 接收到消息的回调
  1094. */
  1095. - (void)didReceiveMessageNotification:(NSNotification *)notification {
  1096. NSDictionary *dic = notification.object; /// __block TXLiveMessageModel *liveMessage
  1097. NSDictionary *content = [dic ks_dictionaryValueForKey:@"message"];
  1098. NSString *objectName = [content ks_stringValueForKey:@"objectName"];
  1099. NSString *className = [self getMsgClassName:objectName];
  1100. __block TXLiveMessageModel *message = [[NSClassFromString(className) alloc] init];
  1101. message.messageId = [dic ks_stringValueForKey:@"msgID"];
  1102. message.groupId = [dic ks_stringValueForKey:@"groupID"];
  1103. [message evaluateSource:content];
  1104. if ([message.groupId isEqualToString:self.roomId]) {
  1105. __weak typeof(&*self) __blockSelf = self;
  1106. dispatch_async(dispatch_get_main_queue(), ^{
  1107. if ([message isMemberOfClass:[TXLiveMessageLike class]]) {
  1108. TXLiveMessageLike *likeMessage = (TXLiveMessageLike *)message;
  1109. NSLog(@"receive like count ------- %d",likeMessage.counts);
  1110. return;
  1111. }
  1112. else if ([message isMemberOfClass:[TXLiveMessageLikeCount class]]) {
  1113. return;
  1114. }
  1115. else if ([message isMemberOfClass:[TXLiveMessageWelcome class]]) {
  1116. TXLiveMessageWelcome *welcomeMessage = (TXLiveMessageWelcome *)message;
  1117. // 过滤自己发送的欢迎消息
  1118. if ([welcomeMessage.userInfo.sendUserId isEqualToString:UserDefault(IM_USERID)]) {
  1119. return;
  1120. }
  1121. if ([welcomeMessage.userInfo.sendUserId isEqual:__blockSelf.createrId]) { // 如果是老师 重置连麦申请状态
  1122. [__blockSelf resetMicWaitToNomal];
  1123. __blockSelf.isCreateInRoom = YES;
  1124. }
  1125. NSString *userName = [welcomeMessage.userInfo.sendUserName stringByAppendingString:@""];
  1126. NSString *contentMsg = [NSString stringWithFormat:@"%@ 进入直播间",userName];
  1127. [__blockSelf showAnimationView:YES showMessag:contentMsg];
  1128. return;
  1129. }
  1130. // 用户退出消息
  1131. else if ([message isMemberOfClass:[TXLiveMessageLeave class]]) {
  1132. // TXLiveMessageLeave *userLeaveMsg = (TXLiveMessageLeave *)message;
  1133. // if ([userLeaveMsg.userInfo.sendUserId isEqualToString:__blockSelf.createrId]) {
  1134. // __blockSelf.isCreateInRoom = NO;
  1135. // // 下麦
  1136. // if (__blockSelf.micStatus == MICSTATUS_CONNECTING) {
  1137. // [__blockSelf downSeatAction];
  1138. // }
  1139. // }
  1140. return;
  1141. }
  1142. // 用户退出回调
  1143. else if ([message isMemberOfClass:[TXLiveMessageUserQuit class]]) {
  1144. // TXLiveMessageUserQuit *userLeaveMsg = (TXLiveMessageUserQuit *)message;
  1145. // if ([userLeaveMsg.targetId isEqualToString:__blockSelf.createrId]) {
  1146. // __blockSelf.isCreateInRoom = NO;
  1147. // // 下麦
  1148. // if (__blockSelf.micStatus == MICSTATUS_CONNECTING) {
  1149. // [__blockSelf downSeatAction];
  1150. // }
  1151. // }
  1152. return;
  1153. }
  1154. else if ([message isMemberOfClass:[TXLiveMessageMemberUp class]]) {
  1155. return;
  1156. }
  1157. else if ([message isMemberOfClass:[TXLiveMessageStatSync class]]) { // 数据同步 不取点赞数
  1158. return;
  1159. }
  1160. else if ([message isMemberOfClass:[TXLiveMessageKickOut class]]) {
  1161. TXLiveMessageKickOut *kickMessage = (TXLiveMessageKickOut *)message;
  1162. NSString *currentUserId = UserDefault(IM_USERID);
  1163. if ([kickMessage.targetId isEqualToString:currentUserId]) {
  1164. __blockSelf.micStatus = MICSTATUS_NOMAL;
  1165. [__blockSelf kickOutLiveRoom];
  1166. }
  1167. return;
  1168. }
  1169. // 禁言控制
  1170. else if ([message isMemberOfClass:[TXLiveMessageChatBan class]]) {
  1171. TXLiveMessageChatBan *banMessage = (TXLiveMessageChatBan *)message;
  1172. if (banMessage.chatBan) { // 禁止发言
  1173. __blockSelf.enableChat = NO;
  1174. }
  1175. else { // 开启发言
  1176. __blockSelf.enableChat = YES;
  1177. }
  1178. [__blockSelf insertMessage:banMessage];
  1179. }
  1180. // 连麦控制消息
  1181. else if ([message isMemberOfClass:[TXLiveMessageSeatControl class]]) {
  1182. TXLiveMessageSeatControl *seatControlMessage = (TXLiveMessageSeatControl *)message;
  1183. if (seatControlMessage.seatBan) { // 禁止连麦
  1184. __blockSelf.enableSeat = NO;
  1185. // 未连麦情况下才重置
  1186. if (__blockSelf.micStatus != MICSTATUS_CONNECTING) {
  1187. // 如果在申请 发送取消申请消息
  1188. if (__blockSelf.micStatus == MICSTATUS_WAITING) {
  1189. [__blockSelf sendSeatMessageApply:NO];
  1190. }
  1191. __blockSelf.micStatus = MICSTATUS_NOMAL;
  1192. }
  1193. [__blockSelf hideApplyView];
  1194. [__blockSelf hideApplyingView];
  1195. }
  1196. else { // 开启连麦
  1197. __blockSelf.enableSeat = YES;
  1198. }
  1199. [__blockSelf insertMessage:seatControlMessage];
  1200. }
  1201. else if ([message isMemberOfClass:[TXLiveMessageSeatApply class]]) {
  1202. // 只处理主讲人发起的连麦邀请或取消操作
  1203. TXLiveMessageSeatApply *seatApplyMessage = (TXLiveMessageSeatApply *)message;
  1204. NSLog(@"----- RCChatroomSeatApply type:%ld audienceName:%@ ",seatApplyMessage.type,seatApplyMessage.audienceName);
  1205. // 只处理和自己相关的消息
  1206. if (![seatApplyMessage.audienceId isEqualToString:UserDefault(IM_USERID)]) {
  1207. // 刷新麦序
  1208. return;
  1209. }
  1210. if (seatApplyMessage.type == SEATHANDLE_INVITE) { // 主讲人发起申请
  1211. NSString *titles = [NSString stringWithFormat:@"%@邀请您连麦",[NSString returnNoNullStringWithString:seatApplyMessage.teacherName]];
  1212. [__blockSelf showInviteAlertMessage:titles];
  1213. }
  1214. else if (seatApplyMessage.type == SEATHANDLE_DISINVITE) { // 主讲人撤回申请
  1215. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"主讲人撤回了连麦申请"];
  1216. [__blockSelf closeAlertView];
  1217. if (__blockSelf.micStatus == MICSTATUS_CONNECTING) {
  1218. [__blockSelf downSeatAction];
  1219. }
  1220. }
  1221. else if (seatApplyMessage.type == SEATHANDLE_KICKSEAT) { // 主讲人断开连麦
  1222. [__blockSelf kickSeatWithMessage:@"主讲人断开连麦"];
  1223. }
  1224. [__blockSelf hideApplyingView];
  1225. [__blockSelf insertMessage:seatApplyMessage];
  1226. }
  1227. else if ([message isMemberOfClass:[TXLiveMessageSeatResponse class]]) { // 只处理主讲人的回应
  1228. TXLiveMessageSeatResponse *seatResponseMessage = (TXLiveMessageSeatResponse *)message;
  1229. NSLog(@"----- RCChatroomSeatResponse type:%ld audienceName:%@ ",seatResponseMessage.type,seatResponseMessage.audienceName);
  1230. if (![seatResponseMessage.audienceId isEqualToString:UserDefault(IM_USERID)]) { // 只处理和自己相关的消息
  1231. return;
  1232. }
  1233. if (seatResponseMessage.type == SEATRESPONSE_TEACHERAPPROVE && __blockSelf.micStatus == MICSTATUS_WAITING) { //
  1234. [__blockSelf hideApplyingView];
  1235. [__blockSelf connectHostWithStatus:YES];
  1236. [__blockSelf insertMessage:seatResponseMessage];
  1237. }
  1238. else if (seatResponseMessage.type == SEATRESPONSE_TEACHERREJECT) {
  1239. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"主讲人拒绝了您的连麦申请"];
  1240. // 处理连麦状态
  1241. [__blockSelf connectHostWithStatus:NO];
  1242. [__blockSelf insertMessage:seatResponseMessage];
  1243. }
  1244. }
  1245. else if ([message isMemberOfClass:[TXLiveMessageDownSeat class]]) { // 成员主动下麦消息
  1246. return;
  1247. }
  1248. else if ([message isMemberOfClass:[TXLiveMessageClose class]]) { // 直播间已关闭
  1249. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"直播已结束"];
  1250. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1251. // 显示直播间关闭状态
  1252. [__blockSelf closeAlertView];
  1253. [__blockSelf displayCloseView];
  1254. [__blockSelf hideApplyingView];
  1255. });
  1256. return;
  1257. }
  1258. else if ([message isMemberOfClass:[TXLiveTextMessage class]]) {
  1259. TXLiveTextMessage *textMessage = (TXLiveTextMessage *)message;
  1260. [__blockSelf insertMessage:textMessage];
  1261. }
  1262. else if ([message isMemberOfClass:[TXLiveMessageCardMessage class]]) {
  1263. TXLiveMessageCardMessage *cardMessage = (TXLiveMessageCardMessage *)message;
  1264. [__blockSelf sendCardMessage:cardMessage.goodsContent];
  1265. }
  1266. else if ([message isMemberOfClass:[TXLiveMessagePauseLive class]]) { // 主讲人暂停直播
  1267. // __blockSelf.isPauseLive = YES;
  1268. // if (__blockSelf.micStatus == MICSTATUS_WAITING) {
  1269. // [__blockSelf sendSeatMessageApply:NO];
  1270. // }
  1271. // [__blockSelf resetNetTips];
  1272. // [__blockSelf resetConnectionStatus];
  1273. // [__blockSelf closeAlertView];
  1274. // [__blockSelf hideApplyingView];
  1275. // [__blockSelf changeEmptyStatusView];
  1276. // [__blockSelf closePlayStatus];
  1277. return;
  1278. }
  1279. else if ([message isMemberOfClass:[TXLiveMessageOpenLive class]]) { // 开始直播
  1280. // __blockSelf.isPauseLive = NO;
  1281. // __blockSelf.isCreateInRoom = YES;
  1282. // [__blockSelf resetNetTips];
  1283. // [__blockSelf hideEmptyView];
  1284. // [__blockSelf startPlay];
  1285. return;
  1286. }
  1287. else if ([message isMemberOfClass:[TXLiveMessageShopRush class]]) { //
  1288. TXLiveMessageShopRush *rushMsg = (TXLiveMessageShopRush *)message;
  1289. NSString *contentMsg = [NSString stringWithFormat:@"%@ 正在抢购",[NSString returnNoNullStringWithString:rushMsg.userInfo.sendUserName]];
  1290. [__blockSelf showAnimationView:NO showMessag:contentMsg];
  1291. }
  1292. else if ([message isMemberOfClass:[TXLiveMessageBlockUser class]]) { // 黑名单
  1293. TXLiveMessageBlockUser *blockMessage = (TXLiveMessageBlockUser *)message;
  1294. if ([blockMessage.userId isEqualToString:UserDefault(IM_USERID)]) {
  1295. __blockSelf.blacklistFlag = YES;
  1296. [__blockSelf resetConnectionStatus];
  1297. }
  1298. }
  1299. else if ([message isMemberOfClass:[TXLiveMessageUnBlockUser class]]) {
  1300. TXLiveMessageUnBlockUser *unBlockMessage = (TXLiveMessageUnBlockUser *)message;
  1301. if ([unBlockMessage.userId isEqualToString:UserDefault(IM_USERID)]) {
  1302. __blockSelf.blacklistFlag = NO;
  1303. }
  1304. }
  1305. else if ([message isMemberOfClass:[TXLiveMessageDownSeatAll class]]) { // 所有麦上用户下麦
  1306. TXLiveMessageDownSeatAll *downSeatMessage = (TXLiveMessageDownSeatAll *)message;
  1307. if (__blockSelf.micStatus == MICSTATUS_CONNECTING) {
  1308. [__blockSelf kickSeatWithMessage:@"主讲人断开连麦"];
  1309. [__blockSelf insertMessage:downSeatMessage];
  1310. }
  1311. }
  1312. else if ([message isMemberOfClass:[TXLiveMessageRejectAllSeat class]]) { // 拒绝所有连麦申请
  1313. TXLiveMessageRejectAllSeat *rejectAllMessage = (TXLiveMessageRejectAllSeat *)message;
  1314. if (__blockSelf.micStatus == MICSTATUS_WAITING) {
  1315. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"主讲人拒绝了您的连麦申请"];
  1316. // 处理连麦状态
  1317. __blockSelf.micStatus = MICSTATUS_NOMAL;
  1318. [__blockSelf insertMessage:rejectAllMessage];
  1319. [__blockSelf hideApplyingView];
  1320. }
  1321. }
  1322. else if ([message isMemberOfClass:[TXControlMemberMic class]]) { // 控制学生麦克风
  1323. TXControlMemberMic *micCtrlMsg = (TXControlMemberMic *)message;
  1324. // 如果是控制当前学生的mic
  1325. if ([micCtrlMsg.userId isEqualToString:UserDefault(IM_USERID)]) {
  1326. [__blockSelf changeCurrentMicStatus:micCtrlMsg.muteMic];
  1327. }
  1328. }
  1329. else if ([message isMemberOfClass:[TXLiveForceKickMsg class]]) {
  1330. TXLiveForceKickMsg *changeMsg = (TXLiveForceKickMsg *)message;
  1331. if (changeMsg.targetIds.count == 0 || changeMsg.targetIds == nil) { // 如果是全员
  1332. [__blockSelf courseChangeTips:changeMsg];
  1333. }
  1334. else {
  1335. for (NSString *userId in changeMsg.targetIds) { // 如果部分学员中包含自己
  1336. if ([userId isEqualToString:UserDefault(IM_USERID)]) {
  1337. [__blockSelf courseChangeTips:changeMsg];
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. }
  1343. });
  1344. }
  1345. }
  1346. - (void)courseChangeTips:(TXLiveForceKickMsg *)msg {
  1347. // 显示直播间关闭状态
  1348. [self closeAlertView];
  1349. [self displayCloseView];
  1350. [self hideApplyingView];
  1351. // 退出房间
  1352. NSString *tipsMsg = [NSString isEmptyString:msg.reason] ? @"当前课程已调整,请退出直播间" : msg.reason;
  1353. MJWeakSelf;
  1354. self.closeAlert = [LiveRoomConfirmAlert liveroomAlertWithTitle:tipsMsg sureTitle:@"确定" inView:self.view confirm:^{
  1355. [weakSelf exitRoom];
  1356. }];
  1357. }
  1358. - (void)changeCurrentMicStatus:(BOOL)isMute {
  1359. if (isMute == !self.micEnable) { // 如果麦克风状态和当前状态一致,剔除
  1360. return;
  1361. }
  1362. if (_trtcCloud && self.isAudience == NO) {
  1363. self.micEnable = !self.micEnable;
  1364. [self.trtcCloud muteLocalAudio:!self.micEnable];
  1365. NSString *desc = @"";
  1366. if (self.micEnable) {
  1367. desc = @"主讲人已将您的麦克风开启";
  1368. }
  1369. else {
  1370. desc = @"主讲人已将您的麦克风关闭";
  1371. }
  1372. [LOADING_MANAGER MBShowAUTOHidingInWindow:desc];
  1373. [self syncMicStatus:!self.micEnable];
  1374. }
  1375. }
  1376. - (void)didReceiveGroupMessage:(NSNotification *)notification {
  1377. NSDictionary *dic = notification.object; /// __block TXLiveMessageModel *liveMessage
  1378. NSString *groupId = [dic ks_stringValueForKey:@"groupID"];
  1379. NSDictionary *content = [dic ks_dictionaryValueForKey:@"message"];
  1380. if ([groupId isEqualToString:self.roomId]) {
  1381. [self evaluateGroupInfo:content];
  1382. }
  1383. }
  1384. - (void)evaluateGroupInfo:(NSDictionary *)groupInfo {
  1385. // 主播在房间
  1386. if ([[groupInfo ks_stringValueForKey:@"LIVE_STATUS"] isEqualToString:@"ON"]) { // 直播中
  1387. self.isPauseLive = NO;
  1388. if (self.isAudience) {
  1389. [self startPlay];
  1390. }
  1391. }
  1392. else if ([[groupInfo ks_stringValueForKey:@"LIVE_STATUS"] isEqualToString:@"OFF"]) { // 暂停直播
  1393. self.isPauseLive = YES;
  1394. [self closePlayStatus];
  1395. }
  1396. // 摄像头状态
  1397. if ([[groupInfo ks_stringValueForKey:@"ANCHOR_CAMERA"] isEqualToString:@"OFF"]) {
  1398. self.isCreaterVideoEnable = NO;
  1399. }
  1400. else {
  1401. self.isCreaterVideoEnable = YES;
  1402. }
  1403. // 禁言状态
  1404. if ([[groupInfo ks_stringValueForKey:@"GLOBAL_BAN"] isEqualToString:@"ON"]) {
  1405. self.enableChat = NO;
  1406. }
  1407. else {
  1408. self.enableChat = YES;
  1409. }
  1410. // 在线人数
  1411. if ([[groupInfo allKeys] containsObject:@"MEMBER_ONLINE"]) {
  1412. self.totalCount = [groupInfo ks_integerValueForKey:@"MEMBER_ONLINE"];
  1413. [self countMemberCount];
  1414. }
  1415. [self changeEmptyStatusView];
  1416. // 主播全员闭麦状态
  1417. if ([[groupInfo allKeys] containsObject:@"ANCHOR_MIC"]) {
  1418. if ([[groupInfo ks_stringValueForKey:@"ANCHOR_MIC"] isEqualToString:@"ON"]) { // 禁麦
  1419. if (self.muteRoomMic == NO) {
  1420. self.muteRoomMic = YES;
  1421. }
  1422. }
  1423. else {
  1424. if (self.muteRoomMic == YES) {
  1425. self.muteRoomMic = NO;
  1426. }
  1427. }
  1428. }
  1429. else {
  1430. if (self.muteRoomMic == YES) {
  1431. self.muteRoomMic = NO;
  1432. }
  1433. }
  1434. // 禁止连麦
  1435. if ([[groupInfo allKeys] containsObject:@"BAN_LINK_MIC"]) {
  1436. if ([[groupInfo ks_stringValueForKey:@"BAN_LINK_MIC"] isEqualToString:@"ON"]) { // 禁止
  1437. self.enableSeat = NO;
  1438. }
  1439. else {
  1440. self.enableSeat = YES;
  1441. }
  1442. }
  1443. else {
  1444. self.enableSeat = YES;
  1445. }
  1446. }
  1447. - (void)setMuteRoomMic:(BOOL)muteRoomMic {
  1448. _muteRoomMic = muteRoomMic;
  1449. [self changeCurrentMicStatus:muteRoomMic];
  1450. }
  1451. // 群属性变更信息
  1452. - (void)didReceiveGroupCountMessage:(NSNotification *)notification {
  1453. NSDictionary *dic = notification.object; /// __block TXLiveMessageModel *liveMessage
  1454. NSString *groupId = [dic ks_stringValueForKey:@"groupID"];
  1455. NSDictionary *content = [dic ks_dictionaryValueForKey:@"message"];
  1456. if ([groupId isEqualToString:self.roomId]) {
  1457. [self evaluateGroupCount:content];
  1458. }
  1459. }
  1460. - (void)evaluateGroupCount:(NSDictionary *)groupCount {
  1461. // 点赞数据
  1462. if ([[groupCount allKeys] containsObject:@"LIKES"]) {
  1463. self.likeCount = [groupCount ks_integerValueForKey:@"LIKES"];
  1464. [self countLikeMessageCount];
  1465. }
  1466. }
  1467. - (void)closePlayStatus {
  1468. if (self.micStatus == MICSTATUS_WAITING) {
  1469. [self sendSeatMessageApply:NO];
  1470. }
  1471. [self resetNetTips];
  1472. [self resetConnectionStatus];
  1473. [self closeAlertView];
  1474. [self hideApplyingView];
  1475. if (self.isAudience) {
  1476. [self stopPlay];
  1477. }
  1478. else {
  1479. if (_trtcCloud) {
  1480. [self.trtcCloud stopAllRemoteView];
  1481. [self.trtcCloud stopLocalAudio];
  1482. [self.trtcCloud exitRoom];
  1483. }
  1484. }
  1485. }
  1486. - (void)kickOutLiveRoom {
  1487. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您已被踢出直播间"];
  1488. @weakObj(self);
  1489. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1490. @strongObj(self);
  1491. [self quitRoom];
  1492. });
  1493. }
  1494. #pragma mark ------ message operation
  1495. - (void)resetConnectionStatus {
  1496. if (self.micStatus == MICSTATUS_CONNECTING) {
  1497. @weakObj(self);
  1498. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1499. @strongObj(self);
  1500. [self sendDownSeatMsg];
  1501. [self connectHostWithStatus:NO];
  1502. });
  1503. }
  1504. else {
  1505. self.micStatus = MICSTATUS_NOMAL;
  1506. [self removeSeatContainer];
  1507. }
  1508. }
  1509. - (void)resetMicWaitToNomal {
  1510. if (self.micStatus == MICSTATUS_WAITING) {
  1511. self.micStatus = MICSTATUS_NOMAL;
  1512. }
  1513. }
  1514. - (void)tipsTeacherLeaveRoom {
  1515. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"主讲人已离开房间"];
  1516. }
  1517. - (void)hideApplyView {
  1518. if (self.applyView.isShow) {
  1519. [self.applyView hideView];
  1520. }
  1521. }
  1522. - (void)hideApplyingView {
  1523. if (self.applyingView.isShow) {
  1524. [self.applyingView hideView];
  1525. }
  1526. }
  1527. - (void)closeAlertView {
  1528. if (self.alertView && self.alertView.isShow) {
  1529. if (self.alertView.isQuitAlert == NO) {
  1530. [self.alertView removeFromSuperview];
  1531. self.alertView = nil;
  1532. }
  1533. }
  1534. }
  1535. - (void)displayCloseView {
  1536. self.isCloseRoom = YES;
  1537. [self hideTipsView];
  1538. // 移除连麦视图
  1539. [self removeSeatContainer];
  1540. if (self.micStatus != MICSTATUS_NOMAL) {
  1541. self.micStatus = MICSTATUS_NOMAL;
  1542. if (_trtcCloud) {
  1543. [self.trtcCloud stopLocalAudio];
  1544. [self.trtcCloud exitRoom];
  1545. }
  1546. }
  1547. [self stopPlay];
  1548. self.videoView.hidden = YES;
  1549. self.endView.hidden = NO;
  1550. [self setDefaultBottomViewStatus];
  1551. [[NSNotificationCenter defaultCenter] postNotificationName:@"LiveroomClose" object:nil];
  1552. }
  1553. - (void)sendCardMessage:(NSArray *)content {
  1554. // if (_webContainer) {
  1555. // NSMutableDictionary *sendParm = [NSMutableDictionary dictionary];
  1556. // [sendParm setValue:@"cardChange" forKey:@"api"];
  1557. // [sendParm setValue:content forKey:@"content"];
  1558. // [self.webContainer postMessage:sendParm];
  1559. // }
  1560. }
  1561. - (void)showInviteAlertMessage:(NSString *)tipsMessage {
  1562. if (self.alertView && self.alertView.isShow) {
  1563. [self.alertView removeFromSuperview];
  1564. self.alertView = nil;
  1565. }
  1566. MJWeakSelf;
  1567. self.alertView = [LiveAlertView shareInstance];
  1568. [self.alertView alertWithTitle:@"连麦邀请" desc:tipsMessage leftButtonTitle:@"取消" rightTitle:@"确定" inView:self.view cancel:^{
  1569. weakSelf.micStatus = MICSTATUS_NOMAL;
  1570. [weakSelf sendSeatResponseMessage:NO];
  1571. } confirm:^{
  1572. [weakSelf sendSeatResponseMessage:YES];
  1573. }];
  1574. }
  1575. - (void)sendSeatResponseMessage:(BOOL)isApprove {
  1576. SEATRESPONSE type = isApprove ? SEATRESPONSE_AUDIENCEAPPROVE : SEATRESPONSE_AUDIENCEREJECT;
  1577. TXLiveMessageSeatResponse *responseMessage = [[TXLiveMessageSeatResponse alloc] init];
  1578. responseMessage.userInfo = [self getCurrentUser];
  1579. responseMessage.type = type;
  1580. responseMessage.audienceId = UserDefault(IM_USERID);
  1581. responseMessage.audienceName = UserDefault(NicknameKey);
  1582. MJWeakSelf;
  1583. [self sendCustomMessage:responseMessage priority:V2TIM_PRIORITY_HIGH displayMessage:YES callback:^(BOOL success) {
  1584. if (success) {
  1585. if (isApprove) {
  1586. [weakSelf connectHostWithStatus:YES];
  1587. }
  1588. }
  1589. }];
  1590. }
  1591. - (void)kickSeatWithMessage:(NSString *)message {
  1592. [LOADING_MANAGER MBShowAUTOHidingInWindow:message];
  1593. [self sendDownSeatMsg];
  1594. [self connectHostWithStatus:NO];
  1595. }
  1596. - (void)insertMessage:(TXLiveMessageModel *)liveMessage {
  1597. [self appendAndDisplayMessage:liveMessage];
  1598. }
  1599. #pragma mark table data source
  1600. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  1601. return self.conversationDataRepository.count;
  1602. }
  1603. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  1604. TXLiveMessageModel *model = [self.conversationDataRepository objectAtIndex:indexPath.row];
  1605. TXLiveChatListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TXLiveChatListCell"];
  1606. if (!cell) {
  1607. cell = [[TXLiveChatListCell appearance] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TXLiveChatListCell"];
  1608. }
  1609. if ([model isMemberOfClass:[TXConstMessage class]] || [model isMemberOfClass:[TXLiveTextMessage class]] || [model isMemberOfClass:[TXLiveMessageWelcome class]] || [model isMemberOfClass:[TXLiveMessageLike class]] || [model isMemberOfClass:[TXLiveMessageSeatControl class]] ||[model isMemberOfClass:[TXLiveMessageChatBan class]] || [model isMemberOfClass:[TXLiveMessageSeatApply class]] || [model isMemberOfClass:[TXLiveMessageSeatResponse class]] || [model isMemberOfClass:[TXLiveMessageShopRush class]] || [model isMemberOfClass:[TXLiveMessageDownSeatAll class]] || [model isMemberOfClass:[TXLiveMessageRejectAllSeat class]] ){
  1610. [cell setDataModel:model createrId:self.createrId];
  1611. }
  1612. return cell;
  1613. }
  1614. #pragma mark ----- lazying
  1615. - (TRTCCloud *)trtcCloud {
  1616. if (!_trtcCloud) {
  1617. _trtcCloud = [TRTCCloud sharedInstance];
  1618. [_trtcCloud addDelegate:self];
  1619. }
  1620. return _trtcCloud;
  1621. }
  1622. - (V2TXLivePlayer *)livePlayer {
  1623. if (!_livePlayer) {
  1624. _livePlayer = [[V2TXLivePlayer alloc] init];
  1625. [_livePlayer enableReceiveSeiMessage:YES payloadType:243];
  1626. [_livePlayer setObserver:self];
  1627. }
  1628. return _livePlayer;
  1629. }
  1630. - (UIView *)videoContainerView {
  1631. if (!_videoContainerView) {
  1632. _videoContainerView = [[UIView alloc] initWithFrame:CGRectZero];
  1633. _videoContainerView.backgroundColor = [UIColor clearColor];
  1634. }
  1635. return _videoContainerView;
  1636. }
  1637. - (UIView *)videoView {
  1638. if (!_videoView) {
  1639. _videoView = [[UIView alloc] initWithFrame:CGRectZero];
  1640. _videoView.backgroundColor = HexRGB(0x1a1a1a);
  1641. }
  1642. return _videoView;
  1643. }
  1644. - (TXLiveVideoView *)videoRenderView {
  1645. if (!_videoRenderView) {
  1646. _videoRenderView = [[TXLiveVideoView alloc] initWithFrame:CGRectZero];
  1647. }
  1648. return _videoRenderView;
  1649. }
  1650. - (LiveRoomHeadView *)headView {
  1651. if (!_headView) {
  1652. _headView = [LiveRoomHeadView shareInstance];
  1653. MJWeakSelf;
  1654. [_headView quitAction:^{
  1655. [weakSelf showQuitAlert];
  1656. }];
  1657. }
  1658. return _headView;
  1659. }
  1660. - (void)showQuitAlert {
  1661. if (self.isCloseRoom) { // 如果直播结束 直接点击退出房间
  1662. [self quitRoom];
  1663. }
  1664. else if (self.micStatus == MICSTATUS_CONNECTING) {
  1665. [self quitAction];
  1666. }
  1667. else {
  1668. MJWeakSelf;
  1669. self.alertView = [LiveAlertView shareInstance];
  1670. self.alertView.isQuitAlert = YES;
  1671. [self.alertView alertWithTitle:@"确定要退出直播间吗?" desc:@"退出后如再次观看可重新进入~" leftButtonTitle:@"取消" rightTitle:@"确认" inView:self.view cancel:^{
  1672. } confirm:^{
  1673. [weakSelf quitAction];
  1674. }];
  1675. }
  1676. }
  1677. - (void)quitRoom {
  1678. KSDragWindowManager *manager = [KSDragWindowManager sharedManager];
  1679. if (manager.hasShowWindow) {
  1680. [manager resignDragWindow];
  1681. }
  1682. if (self.landScape) {
  1683. [self changeOrientation:!self.landScape];
  1684. @weakObj(self);
  1685. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1686. @strongObj(self);
  1687. // 退出房间
  1688. [self exitRoom];
  1689. });
  1690. }
  1691. else {
  1692. // 退出房间
  1693. [self exitRoom];
  1694. }
  1695. }
  1696. // 顶部退出房间
  1697. - (void)quitAction {
  1698. if (self.micStatus == MICSTATUS_CONNECTING) {
  1699. MJWeakSelf;
  1700. self.alertView = [LiveAlertView shareInstance];
  1701. [self.alertView alertWithTitle:@"提示" desc:@"连麦中,是否退出房间?" leftButtonTitle:@"取消" rightTitle:@"确定" inView:self.view cancel:^{
  1702. } confirm:^{
  1703. [weakSelf quitRoom];
  1704. }];
  1705. }
  1706. else {
  1707. [self quitRoom];
  1708. }
  1709. }
  1710. - (void)exitRoom {
  1711. if (self.isAudience) {
  1712. [self stopPlay];
  1713. }
  1714. else {
  1715. if (_trtcCloud) {
  1716. [self.trtcCloud stopLocalAudio];
  1717. [self.trtcCloud exitRoom];
  1718. self.trtcCloud = nil;
  1719. }
  1720. }
  1721. TXLiveMessageLeave *leaveMsg = [[TXLiveMessageLeave alloc] init];
  1722. leaveMsg.userInfo = [self getCurrentUser];
  1723. MJWeakSelf;
  1724. [self sendCustomMessage:leaveMsg priority:V2TIM_PRIORITY_NORMAL displayMessage:NO callback:^(BOOL success) {
  1725. // quit 接口
  1726. [weakSelf quitNotiferService];
  1727. [weakSelf quitChatRoom];
  1728. [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
  1729. }];
  1730. }
  1731. - (void)quitChatRoom {
  1732. [[V2TIMManager sharedInstance] quitGroup:self.roomId succ:^{
  1733. } fail:^(int code, NSString *desc) {
  1734. }];
  1735. }
  1736. - (LiveRoomBottomView *)bottomView {
  1737. if (!_bottomView) {
  1738. _bottomView = [LiveRoomBottomView shareInstance];
  1739. MJWeakSelf;
  1740. [_bottomView bottomClickAction:^(LIVEROOMACTION action) {
  1741. [weakSelf bottomViewAction:action];
  1742. }];
  1743. }
  1744. return _bottomView;
  1745. }
  1746. - (void)bottomViewAction:(LIVEROOMACTION)action {
  1747. if (self.isCloseRoom) {
  1748. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"直播已结束"];
  1749. return;
  1750. }
  1751. if (action == LIVEROOMACTION_CHAT) {
  1752. if (self.blacklistFlag == YES) {
  1753. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您已被管理员禁言"];
  1754. return;
  1755. }
  1756. if (self.enableChat) { // 判断是否全员禁言
  1757. [_inputBar setHidden:NO];
  1758. [_inputBar setInputBarStatus:KSBottomBarStatusKeyboard];
  1759. }
  1760. else {
  1761. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"管理员关闭聊天"];
  1762. }
  1763. }
  1764. else if (action == LIVEROOMACTION_SEAT) { // 连麦按钮
  1765. // 判断是否是连麦状态
  1766. if (self.micStatus == MICSTATUS_CONNECTING) { // 连麦中
  1767. // 是否取消连麦
  1768. MJWeakSelf;
  1769. self.alertView = [LiveAlertView shareInstance];
  1770. [self.alertView alertWithTitle:@"连麦中,确认取消连麦吗?" desc:@"断开后如想再次连麦可重新发起" leftButtonTitle:@"取消" rightTitle:@"确定关闭" inView:self.view cancel:^{
  1771. } confirm:^{
  1772. [weakSelf downSeatAction];
  1773. }];
  1774. }
  1775. else if (self.micStatus == MICSTATUS_WAITING) { // 连麦申请中
  1776. // 弹窗取消申请
  1777. [self displayApplyingView];
  1778. }
  1779. else {
  1780. // 禁止聊天可以连麦(测试确认)
  1781. if (self.blacklistFlag == YES) {
  1782. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您已被管理员禁言"];
  1783. return;
  1784. }
  1785. if (self.isPauseLive == YES) {
  1786. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"老师暂停中,暂时无法接受申请"];
  1787. return;
  1788. }
  1789. if (self.enableSeat) {
  1790. [self displaySeatApplyView];
  1791. }
  1792. else {
  1793. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"管理员关闭连麦申请"];
  1794. }
  1795. }
  1796. }
  1797. else if (action == LIVEROOMACTION_LIKE) { // 点赞
  1798. [self praiseBtnPressed];
  1799. }
  1800. else if (action == LIVEROOMACTION_SHOP) { // shop
  1801. [self showCartAlert];
  1802. }
  1803. else if (action == LIVEROOMACTION_MUTE) {
  1804. [self muteVoiceAction];
  1805. }
  1806. }
  1807. - (void)showCartAlert {
  1808. [self.cardView showViewInView:self.view];
  1809. }
  1810. - (void)downSeatAction {
  1811. [self sendDownSeatMsg];
  1812. [self connectHostWithStatus:NO];
  1813. }
  1814. - (void)sendDownSeatMsg {
  1815. TXLiveMessageDownSeat *downSeatMsg = [[TXLiveMessageDownSeat alloc] init];
  1816. TXLiveUser *mineInfo = [self getCurrentUser];
  1817. downSeatMsg.userInfo = mineInfo;
  1818. downSeatMsg.audienceId = mineInfo.sendUserId;
  1819. downSeatMsg.audienceName = mineInfo.sendUserName;
  1820. [self sendCustomMessage:downSeatMsg priority:V2TIM_PRIORITY_HIGH displayMessage:NO callback:^(BOOL success) {
  1821. }];
  1822. }
  1823. - (KSShopCardView *)cardView {
  1824. if (!_cardView) {
  1825. _cardView = [KSShopCardView shareInstance];
  1826. MJWeakSelf;
  1827. [_cardView clickAction:^(CART_TYPE type) {
  1828. [weakSelf displayTeacherInfo:type];
  1829. }];
  1830. }
  1831. return _cardView;
  1832. }
  1833. // tabs= practice | 陪练课 live | 直播课 video | 视频课 music | 乐谱 跳转到对应tab页
  1834. - (void)displayTeacherInfo:(CART_TYPE)type {
  1835. [self sendRushMessage];
  1836. if (self.isPauseLive == NO) {
  1837. _hasShowSuspendView = YES;
  1838. KSDragWindowManager *manager = [KSDragWindowManager sharedManager];
  1839. BOOL isPhoneLive = self.needSwitchButton ? NO : YES;
  1840. [manager initDragWindowWithPhoneLive:isPhoneLive];
  1841. manager.rootVC = (CustomNavViewController *)self.navigationController;
  1842. [self.videoRenderView removeFromSuperview];
  1843. [manager.dragWindow.containerView addSubview:self.videoRenderView];
  1844. [self.videoRenderView mas_updateConstraints:^(MASConstraintMaker *make) {
  1845. make.left.right.top.bottom.mas_equalTo(manager.dragWindow.containerView);
  1846. }];
  1847. [manager.dragWindow bringSubviewToFront:manager.dragWindow.cancelButton];
  1848. }
  1849. NSString *tabs = @"";
  1850. switch (type) {
  1851. case CART_TYPE_NONE:
  1852. {
  1853. tabs = @"";
  1854. }
  1855. break;
  1856. case CART_TYPE_ACCOMPANY:
  1857. {
  1858. tabs = @"practice";
  1859. }
  1860. break;
  1861. case CART_TYPE_LIVE:
  1862. {
  1863. tabs = @"live";
  1864. }
  1865. break;
  1866. case CART_TYPE_VIDEO:
  1867. {
  1868. tabs = @"video";
  1869. }
  1870. break;
  1871. case CART_TYPE_MUSIC:
  1872. {
  1873. tabs = @"music";
  1874. }
  1875. break;
  1876. default:
  1877. break;
  1878. }
  1879. NSString *url = @"";
  1880. if ([NSString isEmptyString:tabs]) {
  1881. url = [NSString stringWithFormat:@"%@%@%@", WEBHOST,@"/#/teacherHome?teacherId=",self.createrUserId];
  1882. }
  1883. else {
  1884. url = [NSString stringWithFormat:@"%@%@%@&tabs=%@", WEBHOST,@"/#/teacherHome?teacherId=",self.createrUserId,tabs];
  1885. }
  1886. KSLiveWebViewController *ctrl = [[KSLiveWebViewController alloc] init];
  1887. ctrl.url = url;
  1888. [self.navigationController pushViewController:ctrl animated:YES];
  1889. }
  1890. - (void)sendRushMessage {
  1891. TXLiveMessageShopRush *rushMessage = [[TXLiveMessageShopRush alloc] init];
  1892. rushMessage.userInfo = [self getCurrentUser];
  1893. [self sendCustomMessage:rushMessage priority:V2TIM_PRIORITY_DEFAULT displayMessage:NO callback:^(BOOL success) {
  1894. }];
  1895. }
  1896. - (void)toDetailView:(NSString *)linkUrl {
  1897. [self sendRushMessage];
  1898. if (self.isPauseLive == NO) {
  1899. _hasShowSuspendView = YES;
  1900. KSDragWindowManager *manager = [KSDragWindowManager sharedManager];
  1901. BOOL isPhoneLive = self.needSwitchButton ? NO : YES;
  1902. [manager initDragWindowWithPhoneLive:isPhoneLive];
  1903. manager.rootVC = (CustomNavViewController *)self.navigationController;
  1904. [self.videoRenderView removeFromSuperview];
  1905. [manager.dragWindow.containerView addSubview:self.videoRenderView];
  1906. [self.videoRenderView mas_updateConstraints:^(MASConstraintMaker *make) {
  1907. make.left.right.top.bottom.mas_equalTo(manager.dragWindow.containerView);
  1908. }];
  1909. [manager.dragWindow bringSubviewToFront:manager.dragWindow.cancelButton];
  1910. }
  1911. KSLiveWebViewController *ctrl = [[KSLiveWebViewController alloc] init];
  1912. ctrl.url = linkUrl;
  1913. [self.navigationController pushViewController:ctrl animated:YES];
  1914. }
  1915. - (void)muteVoiceAction {
  1916. if (self.bottomView.isMute) {
  1917. if ([self.livePlayer isPlaying]) {
  1918. [self.livePlayer setPlayoutVolume:0];
  1919. }
  1920. if (_trtcCloud) {
  1921. [self.trtcCloud muteAllRemoteAudio:YES];
  1922. }
  1923. }
  1924. else {
  1925. if ([self.livePlayer isPlaying]) {
  1926. [self.livePlayer setPlayoutVolume:100];
  1927. }
  1928. if (_trtcCloud) {
  1929. [self.trtcCloud muteAllRemoteAudio:NO];
  1930. }
  1931. }
  1932. }
  1933. - (KSLiveEmptyView *)emptyView {
  1934. if (!_emptyView) {
  1935. _emptyView = [[KSLiveEmptyView alloc] init];
  1936. BOOL isPhoneLive = self.needSwitchButton ? NO : YES;
  1937. [_emptyView dispPlayViewIsPhoneLive:isPhoneLive];
  1938. }
  1939. return _emptyView;
  1940. }
  1941. - (KSLiveEndView *)endView {
  1942. if (!_endView) {
  1943. _endView = [[KSLiveEndView alloc] init];
  1944. _endView.backgroundColor = HexRGB(0x25292e);
  1945. BOOL isPhoneLive = self.needSwitchButton ? NO : YES;
  1946. [_endView dispPlayViewIsPhoneLive:isPhoneLive];
  1947. }
  1948. return _endView;
  1949. }
  1950. - (KSLiveBadNetView *)netBadView {
  1951. if (!_netBadView) {
  1952. _netBadView = [[KSLiveBadNetView alloc] init];
  1953. BOOL isPhoneLive = self.needSwitchButton ? NO : YES;
  1954. [_netBadView dispPlayViewIsPhoneLive:isPhoneLive];
  1955. MJWeakSelf;
  1956. [_netBadView configViewWithImageName:@"liveVideo_close" desc:@"视频加载失败" callback:^{
  1957. [weakSelf resetAllConnection];
  1958. }];
  1959. }
  1960. return _netBadView;
  1961. }
  1962. - (void)resetNetTips {
  1963. [self hideTipsView];
  1964. }
  1965. - (void)showBadNetTips {
  1966. dispatch_main_async_safe(^{
  1967. if (self.isCloseRoom == NO) {
  1968. if (self.netBadView.hidden == YES) {
  1969. self.netBadView.hidden = NO;
  1970. self.videoView.hidden = YES;
  1971. }
  1972. }
  1973. });
  1974. }
  1975. - (void)hideTipsView {
  1976. dispatch_main_async_safe(^{
  1977. if (self.netBadView.hidden == NO) {
  1978. self.netBadView.hidden = YES;
  1979. self.videoView.hidden = NO;
  1980. }
  1981. })
  1982. }
  1983. // 黑名单
  1984. - (void)setBlacklistFlag:(BOOL)blacklistFlag {
  1985. _blacklistFlag = blacklistFlag;
  1986. [self refreshBottomChatStatus];
  1987. }
  1988. - (void)refreshBottomChatStatus {
  1989. NSString *tipsTitle = self.blacklistFlag ? @"您已被管理员禁言" : @"快来互动吧!";
  1990. self.bottomView.tipsLabel.text = tipsTitle;
  1991. self.bottomView.tipsLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  1992. }
  1993. - (void)resetAllConnection {
  1994. [self resetNetTips];
  1995. // 清理视图
  1996. [self hideEmptyView];
  1997. [self removeSeatContainer];
  1998. // 立刻房间后重新加入房间
  1999. [self startPlay];
  2000. }
  2001. - (LOTAnimationView *)animationView {
  2002. if (!_animationView) {
  2003. _animationView = [LOTAnimationView animationWithFilePath:[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"LiveCard" ofType:@"bundle"]] pathForResource:@"cardAnimation" ofType:@"json"]];
  2004. _animationView.loopAnimation = YES;
  2005. }
  2006. return _animationView;
  2007. }
  2008. - (UIButton *)switchButton {
  2009. if (!_switchButton) {
  2010. _switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  2011. [_switchButton setImage:[UIImage imageNamed:@"live_switch"] forState:UIControlStateNormal];
  2012. [_switchButton addTarget:self action:@selector(transferToLandscape) forControlEvents:UIControlEventTouchUpInside];
  2013. }
  2014. return _switchButton;
  2015. }
  2016. - (UIView *)messageContentView {
  2017. if (!_messageContentView) {
  2018. _messageContentView = [[UIView alloc] init];
  2019. [_messageContentView setBackgroundColor: [UIColor clearColor]];
  2020. }
  2021. return _messageContentView;
  2022. }
  2023. - (UITableView *)conversationMessageTableView {
  2024. if (!_conversationMessageTableView) {
  2025. _conversationMessageTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  2026. _conversationMessageTableView.backgroundColor = [UIColor clearColor];
  2027. _conversationMessageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  2028. _conversationMessageTableView.dataSource = self;
  2029. _conversationMessageTableView.delegate = self;
  2030. _conversationMessageTableView.rowHeight = UITableViewAutomaticDimension;
  2031. _conversationMessageTableView.estimatedRowHeight = 40.0f;
  2032. _conversationMessageTableView.showsVerticalScrollIndicator = NO;
  2033. [_conversationMessageTableView registerClass:[TXLiveChatListCell class] forCellReuseIdentifier:@"TXLiveChatListCell"];
  2034. }
  2035. return _conversationMessageTableView;
  2036. }
  2037. - (NSMutableArray<TXLiveMessageModel *> *)conversationDataRepository {
  2038. if (!_conversationDataRepository) {
  2039. _conversationDataRepository = [NSMutableArray array];
  2040. }
  2041. return _conversationDataRepository;
  2042. }
  2043. - (TXSeatContainerView *)seatContainer {
  2044. if (!_seatContainer) {
  2045. _seatContainer = [[TXSeatContainerView alloc] init];
  2046. MJWeakSelf;
  2047. [_seatContainer seatActionCallback:^(NSString * _Nonnull userId, BOOL muteMic) {
  2048. [weakSelf muteUserAction:userId muteStatus:muteMic];
  2049. }];
  2050. }
  2051. return _seatContainer;
  2052. }
  2053. - (void)muteUserAction:(NSString *)userId muteStatus:(BOOL)muteMic {
  2054. [self micButtonAction:muteMic];
  2055. TXMicStatusSync *ctrlMsg = [[TXMicStatusSync alloc] init];
  2056. ctrlMsg.userInfo = [self getCurrentUser];
  2057. ctrlMsg.userId = userId;
  2058. ctrlMsg.muteMic = muteMic;
  2059. [self sendCustomMessage:ctrlMsg priority:V2TIM_PRIORITY_HIGH displayMessage:NO callback:^(BOOL success) {
  2060. }];
  2061. }
  2062. - (KSChatInputBarControl *)inputBar {
  2063. if (!_inputBar) {
  2064. _inputBar = [[KSChatInputBarControl alloc] initWithStatus:KSBottomBarStatusDefault];
  2065. [_inputBar setDelegate:self];
  2066. }
  2067. return _inputBar;
  2068. }
  2069. - (void)setMicStatus:(MICSTATUS)micStatus {
  2070. _micStatus = micStatus;
  2071. if (self.bottomView) {
  2072. switch (micStatus) {
  2073. case MICSTATUS_NOMAL:
  2074. {
  2075. [self.bottomView.seatButton setImage:[UIImage imageNamed:@"seat_image"] forState:UIControlStateNormal];
  2076. }
  2077. break;
  2078. case MICSTATUS_WAITING:
  2079. {
  2080. [self.bottomView.seatButton setImage:[UIImage imageNamed:@"seat_connecting"] forState:UIControlStateNormal];
  2081. }
  2082. break;
  2083. case MICSTATUS_CONNECTING:
  2084. {
  2085. [self.bottomView.seatButton setImage:[UIImage imageNamed:@"seat_connected"] forState:UIControlStateNormal];
  2086. }
  2087. break;
  2088. default:
  2089. break;
  2090. }
  2091. }
  2092. }
  2093. #pragma mark ----- 人数和点赞数统计
  2094. - (void)countLikeMessageCount {
  2095. NSInteger count = self.likeCount;
  2096. NSString *desc = @"";
  2097. if (count > 10000) {
  2098. count = count / 10000;
  2099. desc = [NSString stringWithFormat:@"本场点赞%zd万+",count];
  2100. }
  2101. else {
  2102. desc = [NSString stringWithFormat:@"本场点赞%zd",count];
  2103. }
  2104. self.headView.likeCount.text = desc;
  2105. }
  2106. - (void)countMemberCount {
  2107. NSInteger count = self.totalCount;
  2108. NSString *desc = @"";
  2109. if (count > 10000) {
  2110. count = count / 10000;
  2111. desc = [NSString stringWithFormat:@"%zd万+人",count];
  2112. }
  2113. else {
  2114. desc = [NSString stringWithFormat:@"%zd人",count];
  2115. }
  2116. self.headView.roomMemberCount.text = desc;
  2117. }
  2118. /**
  2119. * 判断消息是否在collectionView的底部
  2120. *
  2121. * @return 是否在底部
  2122. */
  2123. - (BOOL)isAtTheBottomOfTableView {
  2124. if (self.conversationMessageTableView.contentSize.height <= self.conversationMessageTableView.frame.size.height) {
  2125. return YES;
  2126. }
  2127. if(self.conversationMessageTableView.contentOffset.y +200 >= (self.conversationMessageTableView.contentSize.height - self.conversationMessageTableView.frame.size.height)) {
  2128. return YES;
  2129. }else{
  2130. return NO;
  2131. }
  2132. }
  2133. /**
  2134. * 消息滚动到底部
  2135. *
  2136. * @param animated 是否开启动画效果
  2137. */
  2138. - (void)scrollToBottomAnimated:(BOOL)animated {
  2139. if ([self.conversationMessageTableView numberOfSections] == 0) {
  2140. return;
  2141. }
  2142. NSUInteger finalRow = MAX(0, [self.conversationMessageTableView numberOfRowsInSection:0] - 1);
  2143. if (0 == finalRow) {
  2144. return;
  2145. }
  2146. NSIndexPath *finalIndexPath =
  2147. [NSIndexPath indexPathForItem:finalRow inSection:0];
  2148. [self.conversationMessageTableView scrollToRowAtIndexPath:finalIndexPath atScrollPosition:UITableViewScrollPositionTop animated:animated];
  2149. }
  2150. #pragma mark ---- dealloc
  2151. - (void)dealloc {
  2152. NSLog(@"---- live room dealloc");
  2153. [[NSNotificationCenter defaultCenter] removeObserver:self];
  2154. if (_livePlayer) {
  2155. [self.livePlayer stopPlay];
  2156. _livePlayer = nil;
  2157. }
  2158. if (_trtcCloud) {
  2159. [self.trtcCloud stopAllRemoteView];
  2160. [self.trtcCloud stopLocalAudio];
  2161. [self.trtcCloud exitRoom];
  2162. [self.trtcCloud removeDelegate:self];
  2163. _trtcCloud = nil;
  2164. [TRTCCloud destroySharedInstance];
  2165. }
  2166. }
  2167. #pragma mark - KSChatInputBarControlDelegate
  2168. // 根据inputBar 回调来修改页面布局
  2169. - (void)onInputBarControlContentSizeChanged:(CGRect)frame withAnimationDuration:(CGFloat)duration andAnimationCurve:(UIViewAnimationCurve)curve {
  2170. CGRect originFrame = _inputBar.frame;
  2171. __weak __typeof(&*self)weakSelf = self;
  2172. // 目前只使用y值即可 -- 只修改messageContentView高度,让展示消息view和输入框随之移动
  2173. [UIView animateWithDuration:duration animations:^{
  2174. [UIView setAnimationCurve:curve];
  2175. [weakSelf.inputBar setFrame:CGRectMake(0, frame.origin.y - originFrame.size.height, originFrame.size.width, originFrame.size.height)];
  2176. [UIView commitAnimations];
  2177. }];
  2178. }
  2179. // 发送消息
  2180. - (void)onTouchSendButton:(NSString *)text {
  2181. if (self.blacklistFlag == YES) {
  2182. [self setDefaultBottomViewStatus];
  2183. return;
  2184. }
  2185. NSDate *date = [NSDate date];
  2186. NSTimeInterval currentInterval = [date timeIntervalSince1970];
  2187. if (currentInterval - self.lastSendMsgTime < 3) {
  2188. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您说话太快啦"];
  2189. return;
  2190. }
  2191. if (self.lastSendMsgTime == 0) {
  2192. self.lastSendMsgTime = currentInterval;
  2193. }
  2194. if (text.length > 40) {
  2195. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"聊天消息需在40个字以内哦"];
  2196. return;
  2197. }
  2198. if (self.enableChat) {
  2199. self.lastSendMsgTime = currentInterval;
  2200. [self touristSendMessage:text];
  2201. }
  2202. else {
  2203. [self setDefaultBottomViewStatus];
  2204. }
  2205. }
  2206. - (void)touristSendMessage:(NSString *)text {
  2207. TXLiveTextMessage *message = [[TXLiveTextMessage alloc] init];
  2208. message.userInfo = [self getCurrentUser];
  2209. message.text = text;
  2210. MJWeakSelf;
  2211. [self sendTextMessage:message priority:V2TIM_PRIORITY_NORMAL displayMessage:YES callback:^(BOOL success) {
  2212. [weakSelf setDefaultBottomViewStatus];
  2213. }];
  2214. }
  2215. - (TXLiveUser *)getCurrentUser {
  2216. TXLiveUser *user = [[TXLiveUser alloc] init];
  2217. user.sendUserId = UserDefault(IM_USERID);
  2218. user.sendUserName = UserDefault(NicknameKey);
  2219. user.avatarUrl = UserDefault(AvatarUrlKey);
  2220. return user;
  2221. }
  2222. /**
  2223. 点赞
  2224. */
  2225. - (void)praiseBtnPressed {
  2226. if (self.enableLike == NO) {
  2227. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"管理员已关闭点赞功能"];
  2228. return;
  2229. }
  2230. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  2231. @weakObj(self);
  2232. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.21 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  2233. @strongObj(self);
  2234. if ([[NSDate date] timeIntervalSince1970] - self.lastClickPraiseTime >= 0.2) {
  2235. [self modifyLikeCount:clickPraiseBtnTimes];
  2236. clickPraiseBtnTimes = 0;
  2237. }
  2238. });
  2239. clickPraiseBtnTimes++;
  2240. self.lastClickPraiseTime = currentTime;
  2241. [self presentLikeMessageAnimation];
  2242. }
  2243. - (void)modifyLikeCount:(NSInteger)count {
  2244. [[V2TIMManager sharedInstance] increaseGroupCounter:self.roomId key:@"LIKES" value:count succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {
  2245. } fail:^(int code, NSString *desc) {
  2246. }];
  2247. }
  2248. /**
  2249. 赞动画
  2250. */
  2251. - (void)presentLikeMessageAnimation {
  2252. LiveRoomLikeLayer *layer = [[LiveRoomLikeLayer alloc] initWithName:@"Like"];
  2253. [self.videoContainerView.layer addSublayer:layer];
  2254. CGRect tempFrame = [self.videoContainerView convertRect:self.bottomView.likeButton.frame fromView:self.bottomView];
  2255. CGFloat width = tempFrame.size.width;
  2256. CGFloat height = tempFrame.size.height;
  2257. CGFloat dx = CGRectGetMidX(tempFrame) - width *0.5;
  2258. CGFloat dy = CGRectGetMidY(tempFrame) - 8 - height;
  2259. layer.frame = CGRectMake(dx, dy, width, height);
  2260. [layer startAnimation];
  2261. }
  2262. - (void)displaySeatApplyView {
  2263. [self.applyView showInView:self.view];
  2264. }
  2265. - (void)displayApplyingView {
  2266. [self.applyingView showInView:self.view];
  2267. }
  2268. - (LiveApplyView *)applyView {
  2269. if (!_applyView) {
  2270. _applyView = [LiveApplyView shareInstance];
  2271. MJWeakSelf;
  2272. [_applyView applySeatCallback:^{
  2273. [weakSelf applySeatAction];
  2274. }];
  2275. }
  2276. return _applyView;
  2277. }
  2278. - (void)applySeatAction {
  2279. [self sendSeatMessageApply:YES];
  2280. }
  2281. - (void)sendSeatMessageApply:(BOOL)isApply {
  2282. if (self.micStatus == MICSTATUS_CONNECTING) {
  2283. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您已上麦"];
  2284. return;
  2285. }
  2286. SEATHANDLE type = isApply ? SEATHANDLE_APPLY : SEATHANDLE_CANCELAPPLY;
  2287. TXLiveMessageSeatApply *applyMessage = [[TXLiveMessageSeatApply alloc] init];
  2288. applyMessage.type = type;
  2289. applyMessage.audienceId = UserDefault(IM_USERID);
  2290. applyMessage.audienceName = UserDefault(NicknameKey);
  2291. applyMessage.audienceAvatar = UserDefault(AvatarUrlKey);
  2292. applyMessage.teacherId = self.createrId;
  2293. applyMessage.teacherName = self.createrName;
  2294. applyMessage.userInfo = [self getCurrentUser];
  2295. MJWeakSelf;
  2296. [self sendCustomMessage:applyMessage priority:V2TIM_PRIORITY_HIGH displayMessage:YES callback:^(BOOL success) {
  2297. if (success) {
  2298. weakSelf.micStatus = isApply ? MICSTATUS_WAITING : MICSTATUS_NOMAL;
  2299. }
  2300. }];
  2301. }
  2302. - (LiveApplyingView *)applyingView {
  2303. if (!_applyingView) {
  2304. _applyingView = [LiveApplyingView shareInstance];
  2305. NSString *avatar = UserDefaultObjectForKey(AvatarUrlKey);
  2306. [_applyingView.myAvatal sd_setImageWithURL:[NSURL URLWithString:[avatar getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  2307. [_applyingView.teacherAvatal sd_setImageWithURL:[NSURL URLWithString:[self.createrAvatal getUrlEndcodeString]] placeholderImage:[UIImage imageNamed:USERDEFAULT_LOGO]];
  2308. MJWeakSelf;
  2309. [_applyingView cancleApplyCallback:^{
  2310. [weakSelf cancleApplyAction];
  2311. }];
  2312. }
  2313. return _applyingView;
  2314. }
  2315. - (void)cancleApplyAction {
  2316. MJWeakSelf;
  2317. self.alertView = [LiveAlertView shareInstance];
  2318. [self.alertView alertWithTitle:@"连麦申请中,确认取消连麦申请吗?" desc:@"断开后如想再次连麦可重新发起" leftButtonTitle:@"取消" rightTitle:@"确定关闭" inView:self.view cancel:^{
  2319. } confirm:^{
  2320. [weakSelf.applyingView hideView];
  2321. [weakSelf sendSeatMessageApply:NO];
  2322. }];
  2323. }
  2324. #pragma mark ---- sendMessage/showMessage
  2325. // 文本消息
  2326. - (void)sendTextMessage:(TXLiveMessageModel *)messageContent priority:(V2TIMMessagePriority)priority displayMessage:(BOOL)displayMessage callback:(void(^)(BOOL success))callback {
  2327. NSData *msgData = [messageContent encodeMessage];
  2328. if (msgData == nil) {
  2329. return;
  2330. }
  2331. __weak typeof(&*self) __weakself = self;
  2332. [TXLiveMessageCenter sendLiveChatMessage:msgData groupId:self.roomId priority:priority success:^{
  2333. dispatch_async(dispatch_get_main_queue(), ^{
  2334. if (displayMessage) {
  2335. [__weakself appendAndDisplayMessage:messageContent];
  2336. }
  2337. [__weakself.inputBar clearInputView];
  2338. callback(YES);
  2339. });
  2340. } failer:^(int code, NSString * _Nonnull msg) {
  2341. dispatch_async(dispatch_get_main_queue(), ^{
  2342. [__weakself.inputBar clearInputView];
  2343. NSLog(@"发送失败,errorcode is: %d message: %@",code, msg);
  2344. callback(NO);
  2345. });
  2346. }];
  2347. }
  2348. // 其他消息
  2349. - (void)sendCustomMessage:(TXLiveMessageModel *)messageContent priority:(V2TIMMessagePriority)priority displayMessage:(BOOL)displayMessage callback:(void(^)(BOOL success))callback {
  2350. NSData *msgData = [messageContent encodeMessage];
  2351. if (msgData == nil) {
  2352. return;
  2353. }
  2354. __weak typeof(&*self) __weakself = self;
  2355. [TXLiveMessageCenter sendLiveChatMessage:msgData groupId:self.roomId priority:priority success:^{
  2356. dispatch_async(dispatch_get_main_queue(), ^{
  2357. if (displayMessage) {
  2358. [__weakself appendAndDisplayMessage:messageContent];
  2359. }
  2360. callback(YES);
  2361. });
  2362. } failer:^(int code, NSString * _Nonnull msg) {
  2363. dispatch_async(dispatch_get_main_queue(), ^{
  2364. NSLog(@"发送失败,errorcode is: %d message: %@",code, msg);
  2365. callback(NO);
  2366. });
  2367. }];
  2368. }
  2369. /**
  2370. * 将消息加入本地数组
  2371. */
  2372. - (void)appendAndDisplayMessage:(TXLiveMessageModel *)liveMessage {
  2373. if (!liveMessage) {
  2374. return;
  2375. }
  2376. if ([self appendMessageModel:liveMessage]) {
  2377. NSIndexPath *indexPath =
  2378. [NSIndexPath indexPathForItem:self.conversationDataRepository.count - 1
  2379. inSection:0];
  2380. if ([self.conversationMessageTableView numberOfRowsInSection:0] !=
  2381. self.conversationDataRepository.count - 1) {
  2382. return;
  2383. }
  2384. // view刷新
  2385. [self.conversationMessageTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
  2386. if ([self isAtTheBottomOfTableView] || self.isNeedScrollToButtom) {
  2387. [self scrollToBottomAnimated:NO];
  2388. self.isNeedScrollToButtom=NO;
  2389. }
  2390. }
  2391. return;
  2392. }
  2393. /**
  2394. * 如果当前会话没有这个消息id,把消息加入本地数组
  2395. */
  2396. - (BOOL)appendMessageModel:(TXLiveMessageModel *)model {
  2397. if (!model) {
  2398. return NO;
  2399. }
  2400. //这里可以根据消息类型来决定是否显示,如果不希望显示直接return NO
  2401. //数量不可能无限制的大,这里限制收到消息过多时,就对显示消息数量进行限制。
  2402. //用户可以手动下拉更多消息,查看更多历史消息。
  2403. if (self.conversationDataRepository.count>500) {
  2404. [self.conversationDataRepository removeObjectAtIndex:0];
  2405. [self.conversationMessageTableView reloadData];
  2406. }
  2407. [self.conversationDataRepository addObject:model];
  2408. return YES;
  2409. }
  2410. - (void)showAnimationView:(BOOL)isJoinRoom showMessag:(NSString *)message {
  2411. if (self.landScape) {
  2412. return;
  2413. }
  2414. if (self.enterAnimationView && self.enterAnimationView.isShow) {
  2415. return;
  2416. }
  2417. else {
  2418. ANIMATIONTYPE type = isJoinRoom ? ANIMATIONTYPE_JOIN : ANIMATIONTYPE_RUSH;
  2419. self.enterAnimationView = [[LiveAnimationView alloc] initWithTitle:message animationType:type];
  2420. [self.view addSubview:self.enterAnimationView];
  2421. [self.enterAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
  2422. make.left.right.mas_equalTo(self.view);
  2423. make.width.mas_equalTo(KPortraitWidth);
  2424. make.height.mas_equalTo(24.0f);
  2425. make.bottom.mas_equalTo(self.messageContentView.mas_top).offset(-6);
  2426. }];
  2427. [self.view bringSubviewToFront:self.enterAnimationView];
  2428. MJWeakSelf;
  2429. [self.enterAnimationView startAnimationEndCallback:^{
  2430. weakSelf.enterAnimationView.isShow = NO;
  2431. weakSelf.enterAnimationView = nil;
  2432. }];
  2433. }
  2434. }
  2435. - (NSMutableArray *)remoteMemberArray {
  2436. if (!_remoteMemberArray) {
  2437. _remoteMemberArray = [NSMutableArray array];
  2438. }
  2439. return _remoteMemberArray;
  2440. }
  2441. - (TXLiveLoadingView *)loadingView {
  2442. if (!_loadingView) {
  2443. _loadingView = [TXLiveLoadingView shareInstance];
  2444. }
  2445. return _loadingView;
  2446. }
  2447. - (void)showLoading {
  2448. if (![self.videoView.subviews containsObject:self.loadingView]) {
  2449. [self.loadingView showLoadingView:self.videoView];
  2450. }
  2451. }
  2452. - (void)hideLoading {
  2453. if ([self.videoView.subviews containsObject:self.loadingView]) {
  2454. [self.loadingView hideLoadingView];
  2455. }
  2456. }
  2457. - (void)micButtonAction:(BOOL)isMute {
  2458. if (isMute == !self.micEnable) {
  2459. return;
  2460. }
  2461. if (_trtcCloud && self.isAudience == NO) {
  2462. self.micEnable = !isMute;
  2463. [self.trtcCloud muteLocalAudio:!self.micEnable];
  2464. [self syncMicStatus:!self.micEnable];
  2465. }
  2466. }
  2467. - (void)syncMicStatus:(BOOL)isMute {
  2468. TXMicStatusSync *syncMic = [[TXMicStatusSync alloc] init];
  2469. syncMic.userInfo = [self getCurrentUser];
  2470. syncMic.userId = UserDefault(IM_USERID);
  2471. syncMic.muteMic = isMute;
  2472. [self sendCustomMessage:syncMic priority:V2TIM_PRIORITY_HIGH displayMessage:NO callback:^(BOOL success) {
  2473. }];
  2474. [self renderSeatView];
  2475. }
  2476. #pragma mark ---- 3A config
  2477. - (void)setSubjectId:(NSString *)subjectId {
  2478. _subjectId = subjectId;
  2479. // 设置 3A参数
  2480. self.ANS = 0;
  2481. self.AEC = 100;
  2482. // 长号14和萨克斯5 AGC 一样
  2483. if ([subjectId isEqualToString:@"5"] || [subjectId isEqualToString:@"14"]) {
  2484. self.AGC = 45;
  2485. }
  2486. else if ([subjectId isEqualToString:@"16"]) { // 次中音号
  2487. self.AGC = 80;
  2488. }
  2489. else if ([subjectId isEqualToString:@"12"]) {
  2490. self.AGC = 50;
  2491. }
  2492. else {
  2493. self.AGC = 0;
  2494. }
  2495. }
  2496. /*
  2497. #pragma mark - Navigation
  2498. // In a storyboard-based application, you will often want to do a little preparation before navigation
  2499. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  2500. // Get the new view controller using [segue destinationViewController].
  2501. // Pass the selected object to the new view controller.
  2502. }
  2503. */
  2504. @end