VideoChatVC.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. //
  2. // VideoChatVC.m
  3. // BuguLive
  4. //
  5. // Created by Kylin on 2024/12/3.
  6. // Copyright © 2024 xfg. All rights reserved.
  7. //
  8. #import "VideoChatVC.h"
  9. #import "AudioChatTool.h"
  10. typedef void(^IsNormalChatBlock)(BOOL isNormal);
  11. @interface VideoChatVC ()<TRTCCloudDelegate,AVAudioPlayerDelegate>
  12. @property (nonatomic,strong) UIImageView * headerImgV;
  13. @property (nonatomic,strong) UILabel * nicknameLabel;
  14. @property (nonatomic,strong) UILabel * timesLabel;
  15. @property (nonatomic,strong) TRTCCloud * trtcCloud;
  16. @property (nonatomic,strong) UILabel * tipLabel;
  17. //****************** 通话中View ******************
  18. @property (nonatomic,strong) UIView * chatBotoomView;
  19. @property (nonatomic,strong) UIButton * maikefengButton;
  20. @property (nonatomic,strong) UILabel * maikefengLabel;
  21. //呼叫中,摄像头翻转
  22. @property (nonatomic,strong) UIButton * callingCameraSwitchButton;
  23. @property (nonatomic,strong) UILabel * callingCameraSwitchLabel;
  24. //通话中、呼叫中,摄像头开关
  25. @property (nonatomic,strong) UIButton * chatingCameraOpenButton;
  26. @property (nonatomic,strong) UILabel * chatingCameraOpenLabel;
  27. //通话中,扬声器开关
  28. @property (nonatomic,strong) UIButton * yangshengqiButton;
  29. @property (nonatomic,strong) UILabel * yangshengqiLabel;
  30. //通话中,摄像头翻转
  31. @property (nonatomic,strong) UIButton * chatingCameraSwitchButton;
  32. //通话中,挂断按钮
  33. @property (nonatomic,strong) UIButton * closeButton;
  34. @property (nonatomic,assign) int timesCount;
  35. //****************** 接听中View ******************
  36. @property (nonatomic,strong) UIView * receiveBottomView;
  37. //摄像头翻转按钮及提示
  38. @property (nonatomic,strong) UIButton * receivingCameraSwitchButton;
  39. @property (nonatomic,strong) UILabel * receivingCameraSwitchLabel;
  40. //接听中,摄像头开关
  41. @property (nonatomic,strong) UIButton * receivingCameraOpenButton;
  42. @property (nonatomic,strong) UILabel * receivingCameraOpenLabel;
  43. //接听中,挂断按钮
  44. @property (nonatomic,strong) UIButton * receivingCloseButton;
  45. //接听中,接听按钮
  46. @property (nonatomic,strong) UIButton * receivingReceiveButton;
  47. @property(nonatomic,strong)AVAudioPlayer *player;//播放
  48. @property (nonatomic,assign) int lastMin;
  49. @property (nonatomic,strong) UIView * smallPreviewView;
  50. @property (nonatomic,strong) UIView * bigPreviewView;
  51. @property (nonatomic,copy) IsNormalChatBlock isNormalChatBlock;
  52. @end
  53. @implementation VideoChatVC
  54. static dispatch_once_t onceToken;
  55. static VideoChatVC *_voiceRoomVC;
  56. + (instancetype)shareVoiceRoomVC{
  57. dispatch_once(&onceToken, ^{
  58. _voiceRoomVC = [[VideoChatVC alloc] init];
  59. });
  60. return _voiceRoomVC;
  61. }
  62. /**销毁房间*/
  63. + (void)destoryShareRootVC {
  64. /**更新打开房间的状态*/
  65. AppDelegate.sharedAppDelegate.isInAudioVideoChatVc = NO;
  66. // app.showRoomVc = NO;
  67. // app.IsInVoiceRoom = NO;
  68. NSLog(@"房间销毁了。。。。。。。。。。。1");
  69. /**离开房间*/
  70. // [[TMRoomMsgManager sharedRoomMsgManager] leaveRoomSendMessage];
  71. // [[TMRoomManager sharedInstance] leavingRoom:^(AgoraChannelStats * _Nonnull stat) {}];
  72. // [TMRoomManager destroySharedInstance];
  73. // /**销毁消息*/
  74. // [TMRoomMsgManager destoryRoomMsgManager];
  75. // [_voiceRoomVC logoutGame];
  76. [VideoChatVC.shareVoiceRoomVC closeVoiceRoom];
  77. onceToken = 0;
  78. _voiceRoomVC = nil;
  79. NSLog(@"房间销毁了。。。。。。。。。。。2");
  80. }
  81. - (void)viewWillAppear:(BOOL)animated {
  82. [super viewWillAppear:animated];
  83. self.navigationController.navigationBar.hidden = YES;
  84. }
  85. - (void)viewWillDisappear:(BOOL)animated {
  86. [super viewWillDisappear:animated];
  87. self.navigationController.navigationBar.hidden = NO;
  88. }
  89. - (void)viewDidLoad {
  90. [super viewDidLoad];
  91. AppDelegate.sharedAppDelegate.isInAudioVideoChatVc = YES;
  92. self.lastMin = -1;
  93. //基础UI
  94. [self initChatBaseUI];
  95. // 音量监听
  96. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  97. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
  98. [[NSNotificationCenter defaultCenter] postNotificationName:@"MSG_VIDEO_LINE_CALL" object:nil];
  99. }
  100. - (void)initChatBaseUI {
  101. // UIImageView * bgImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  102. // bgImgV.image = [UIImage imageNamed:@"audioChatbgicon"];
  103. // [self.view addSubview:bgImgV];
  104. _bigPreviewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  105. [self.view addSubview:self.bigPreviewView];
  106. _smallPreviewView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 120 - 20, NavigationHeight + 30, 120, 160)];
  107. self.smallPreviewView.hidden = YES;
  108. self.smallPreviewView.layer.cornerRadius = 10;
  109. self.smallPreviewView.layer.masksToBounds = YES;
  110. self.smallPreviewView.backgroundColor = UIColor.blackColor;
  111. [self.view addSubview:self.smallPreviewView];
  112. UIButton * backVerticalBtn = [[UIButton alloc]initWithFrame:CGRectMake(15,StatusBarHeight, 44, 44)];
  113. [backVerticalBtn setImage:[UIImage imageNamed:@"audioChatSmallIcon"] forState:UIControlStateNormal];
  114. [backVerticalBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
  115. // [self.view addSubview:backVerticalBtn];
  116. _timesLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, SCREEN_WIDTH - 120, 30)];
  117. self.timesLabel.centerY = backVerticalBtn.centerY;
  118. self.timesLabel.textAlignment = NSTextAlignmentCenter;
  119. self.timesLabel.textColor = UIColor.whiteColor;
  120. self.timesLabel.font = [UIFont boldSystemFontOfSize:16];
  121. self.timesLabel.text = @"00:00";
  122. self.timesLabel.hidden = YES;
  123. [self.view addSubview:self.timesLabel];
  124. _headerImgV = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 90)/2, NavigationHeight + 133, 90, 90)];
  125. self.headerImgV.layer.cornerRadius = 10;
  126. self.headerImgV.layer.masksToBounds = YES;
  127. [self.headerImgV sd_setImageWithURL:[NSURL URLWithString:self.mChatFriend.mHead_image] placeholderImage:kDefaultPreloadHeadImg];
  128. [self.view addSubview:self.headerImgV];
  129. _nicknameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headerImgV.bottom + 10, SCREEN_WIDTH, 30)];
  130. self.nicknameLabel.textAlignment = NSTextAlignmentCenter;
  131. self.nicknameLabel.textColor = UIColor.whiteColor;
  132. self.nicknameLabel.font = [UIFont boldSystemFontOfSize:21];
  133. self.nicknameLabel.text = self.mChatFriend.mNick_name;
  134. [self.view addSubview:self.nicknameLabel];
  135. #pragma mark - 接听中UI
  136. _receiveBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - SafeAreaBottomHeight - 20 - 180, SCREEN_WIDTH, 180)];
  137. // self.receiveBottomView.layer.borderColor = UIColor.redColor.CGColor;
  138. // self.receiveBottomView.layer.borderWidth = 1;
  139. self.receiveBottomView.hidden = YES;
  140. [self.view addSubview:self.receiveBottomView];
  141. //摄像头翻转
  142. _receivingCameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  143. self.receivingCameraSwitchButton.frame = CGRectMake(30, 0, 70, 70);
  144. [self.receivingCameraSwitchButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraSwitchIcon"] forState:UIControlStateNormal];
  145. [self.receiveBottomView addSubview:self.receivingCameraSwitchButton];
  146. [self.receivingCameraSwitchButton addTarget:self action:@selector(receivingCameraSwitchButtonClick) forControlEvents:UIControlEventTouchUpInside];
  147. _receivingCameraSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.receivingCameraSwitchButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  148. self.receivingCameraSwitchLabel.centerX = self.receivingCameraSwitchButton.centerX;
  149. self.receivingCameraSwitchLabel.textAlignment = NSTextAlignmentCenter;
  150. self.receivingCameraSwitchLabel.font = [UIFont systemFontOfSize:16];
  151. self.receivingCameraSwitchLabel.textColor = [UIColor colorWithHex:0x999999];
  152. self.receivingCameraSwitchLabel.text = ASLocalizedString(@"摄像头翻转");
  153. [self.receiveBottomView addSubview:self.receivingCameraSwitchLabel];
  154. //摄像头开关
  155. _receivingCameraOpenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  156. self.receivingCameraOpenButton.frame = CGRectMake(SCREEN_WIDTH - 70 - 30, 0, 70, 70);
  157. [self.receivingCameraOpenButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraOpen"] forState:UIControlStateNormal];
  158. [self.receivingCameraOpenButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraClose"] forState:UIControlStateSelected];
  159. [self.receiveBottomView addSubview:self.receivingCameraOpenButton];
  160. [self.receivingCameraOpenButton addTarget:self action:@selector(receivingCameraOpenButtonClick) forControlEvents:UIControlEventTouchUpInside];
  161. _receivingCameraOpenLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.receivingCameraOpenButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  162. self.receivingCameraOpenLabel.centerX = self.receivingCameraOpenButton.centerX;
  163. self.receivingCameraOpenLabel.textAlignment = NSTextAlignmentCenter;
  164. self.receivingCameraOpenLabel.font = [UIFont systemFontOfSize:16];
  165. self.receivingCameraOpenLabel.textColor = [UIColor colorWithHex:0x999999];
  166. self.receivingCameraOpenLabel.text = ASLocalizedString(@"摄像头已开");
  167. [self.receiveBottomView addSubview:self.receivingCameraOpenLabel];
  168. //挂断
  169. _receivingCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  170. self.receivingCloseButton.frame = CGRectMake(self.receivingCameraSwitchButton.left, 0, 70, 70);
  171. self.receivingCloseButton.bottom = self.receiveBottomView.height;
  172. [self.receivingCloseButton setBackgroundImage:[UIImage imageNamed:@"audioclosebtnbg"] forState:UIControlStateNormal];
  173. [self.receiveBottomView addSubview:self.receivingCloseButton];
  174. [self.receivingCloseButton addTarget:self action:@selector(receiveCloseButtonClick) forControlEvents:UIControlEventTouchUpInside];
  175. //接听
  176. _receivingReceiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
  177. self.receivingReceiveButton.frame = CGRectMake(SCREEN_WIDTH - 30 - 70, self.receivingCloseButton.top, 70, 70);
  178. [self.receivingReceiveButton setBackgroundImage:[UIImage imageNamed:@"audioreceivebtnbg"] forState:UIControlStateNormal];
  179. [self.receiveBottomView addSubview:self.receivingReceiveButton];
  180. [self.receivingReceiveButton addTarget:self action:@selector(receivingReceiveButtonClick) forControlEvents:UIControlEventTouchUpInside];
  181. #pragma mark - 通话中UI、呼叫中UI
  182. _chatBotoomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - SafeAreaBottomHeight - 180 - 20, SCREEN_WIDTH, 180)];
  183. // self.chatBotoomView.layer.borderColor = UIColor.redColor.CGColor;
  184. // self.chatBotoomView.layer.borderWidth = 1;
  185. self.chatBotoomView.hidden = YES;
  186. [self.view addSubview:self.chatBotoomView];
  187. //呼叫中,摄像头翻转
  188. _callingCameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  189. self.callingCameraSwitchButton.frame = CGRectMake(30, 0, 70, 70);
  190. [self.callingCameraSwitchButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraSwitchIcon"] forState:UIControlStateNormal];
  191. [self.chatBotoomView addSubview:self.callingCameraSwitchButton];
  192. [self.callingCameraSwitchButton addTarget:self action:@selector(receivingCameraSwitchButtonClick) forControlEvents:UIControlEventTouchUpInside];
  193. _callingCameraSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.callingCameraSwitchButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  194. self.callingCameraSwitchLabel.centerX = self.callingCameraSwitchButton.centerX;
  195. self.callingCameraSwitchLabel.textAlignment = NSTextAlignmentCenter;
  196. self.callingCameraSwitchLabel.font = [UIFont systemFontOfSize:16];
  197. self.callingCameraSwitchLabel.textColor = [UIColor colorWithHex:0x999999];
  198. self.callingCameraSwitchLabel.text = ASLocalizedString(@"摄像头翻转");
  199. [self.chatBotoomView addSubview:self.callingCameraSwitchLabel];
  200. //通话中,麦克风按钮
  201. _maikefengButton = [UIButton buttonWithType:UIButtonTypeCustom];
  202. self.maikefengButton.frame = CGRectMake(30, 0, 70, 70);
  203. [self.maikefengButton setBackgroundImage:[UIImage imageNamed:@"maikefengopen"] forState:UIControlStateNormal];
  204. [self.maikefengButton setBackgroundImage:[UIImage imageNamed:@"maikefengclose"] forState:UIControlStateSelected];
  205. [self.chatBotoomView addSubview:self.maikefengButton];
  206. [self.maikefengButton addTarget:self action:@selector(maikeFengButtonClick) forControlEvents:UIControlEventTouchUpInside];
  207. _maikefengLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.maikefengButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  208. self.maikefengLabel.centerX = self.maikefengButton.centerX;
  209. self.maikefengLabel.textAlignment = NSTextAlignmentCenter;
  210. self.maikefengLabel.font = [UIFont systemFontOfSize:16];
  211. self.maikefengLabel.textColor = [UIColor colorWithHex:0x999999];
  212. self.maikefengLabel.text = ASLocalizedString(@"麦克风已开启");
  213. [self.chatBotoomView addSubview:self.maikefengLabel];
  214. self.maikefengLabel.hidden =
  215. self.maikefengButton.hidden = YES;
  216. //通话中,扬声器开关
  217. _yangshengqiButton = [UIButton buttonWithType:UIButtonTypeCustom];
  218. self.yangshengqiButton.frame = CGRectMake(0, self.maikefengButton.top, 70, 70);
  219. self.yangshengqiButton.centerX = self.chatBotoomView.width/2.0;
  220. [self.yangshengqiButton setBackgroundImage:[UIImage imageNamed:@"yangshengqiclose"] forState:UIControlStateNormal];
  221. [self.yangshengqiButton setBackgroundImage:[UIImage imageNamed:@"yangshengqiopen"] forState:UIControlStateSelected];
  222. [self.chatBotoomView addSubview:self.yangshengqiButton];
  223. [self.yangshengqiButton addTarget:self action:@selector(yangshengqiButtonClick) forControlEvents:UIControlEventTouchUpInside];
  224. _yangshengqiLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.maikefengButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  225. self.yangshengqiLabel.centerX = self.yangshengqiButton.centerX;
  226. self.yangshengqiLabel.textAlignment = NSTextAlignmentCenter;
  227. self.yangshengqiLabel.font = [UIFont systemFontOfSize:16];
  228. self.yangshengqiLabel.textColor = [UIColor colorWithHex:0x999999];
  229. self.yangshengqiLabel.text = ASLocalizedString(@"扬声器已关");
  230. [self.chatBotoomView addSubview:self.yangshengqiLabel];
  231. self.yangshengqiLabel.hidden =
  232. self.yangshengqiButton.hidden = YES;
  233. //通话中,摄像头开关
  234. _chatingCameraOpenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  235. self.chatingCameraOpenButton.frame = CGRectMake(SCREEN_WIDTH - 70 - 30, self.maikefengButton.top, 70, 70);
  236. [self.chatingCameraOpenButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraOpen"] forState:UIControlStateNormal];
  237. [self.chatingCameraOpenButton setBackgroundImage:[UIImage imageNamed:@"receivingCameraClose"] forState:UIControlStateSelected];
  238. [self.chatBotoomView addSubview:self.chatingCameraOpenButton];
  239. [self.chatingCameraOpenButton addTarget:self action:@selector(receivingCameraOpenButtonClick) forControlEvents:UIControlEventTouchUpInside];
  240. _chatingCameraOpenLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.chatingCameraOpenButton.bottom + 10, SCREEN_WIDTH/3, 20)];
  241. self.chatingCameraOpenLabel.centerX = self.chatingCameraOpenButton.centerX;
  242. self.chatingCameraOpenLabel.textAlignment = NSTextAlignmentCenter;
  243. self.chatingCameraOpenLabel.font = [UIFont systemFontOfSize:16];
  244. self.chatingCameraOpenLabel.textColor = [UIColor colorWithHex:0x999999];
  245. self.chatingCameraOpenLabel.text = ASLocalizedString(@"摄像头已开");
  246. [self.chatBotoomView addSubview:self.chatingCameraOpenLabel];
  247. //通话中,挂断按钮
  248. _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  249. self.closeButton.frame = CGRectMake(0, 0, 70, 70);
  250. self.closeButton.bottom = self.chatBotoomView.height;
  251. self.closeButton.centerX = self.chatBotoomView.width/2.0;
  252. [self.closeButton setBackgroundImage:[UIImage imageNamed:@"audioclosebtnbg"] forState:UIControlStateNormal];
  253. [self.chatBotoomView addSubview:self.closeButton];
  254. [self.closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
  255. //通话中,摄像头翻转
  256. _chatingCameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  257. self.chatingCameraSwitchButton.frame = CGRectMake(0, 0, 44, 44);
  258. self.chatingCameraSwitchButton.centerX = self.chatingCameraOpenButton.centerX;
  259. self.chatingCameraSwitchButton.centerY = self.closeButton.centerY;
  260. [self.chatingCameraSwitchButton setBackgroundImage:[UIImage imageNamed:@"chatingCameraSwitchIcon"] forState:UIControlStateNormal];
  261. [self.chatBotoomView addSubview:self.chatingCameraSwitchButton];
  262. [self.chatingCameraSwitchButton addTarget:self action:@selector(receivingCameraSwitchButtonClick) forControlEvents:UIControlEventTouchUpInside];
  263. self.chatingCameraSwitchButton.hidden = YES;
  264. _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.chatBotoomView.top - 60, SCREEN_WIDTH, 30)];
  265. self.tipLabel.textAlignment = NSTextAlignmentCenter;
  266. self.tipLabel.textColor = UIColor.whiteColor;
  267. self.tipLabel.font = [UIFont systemFontOfSize:16];
  268. self.tipLabel.text = ASLocalizedString(@"邀请你视频通话");
  269. self.tipLabel.textColor = [UIColor colorWithHex:0x999999];
  270. self.tipLabel.hidden = YES;
  271. [self.view addSubview:self.tipLabel];
  272. }
  273. #pragma mark - 接听中,点击事件
  274. //接听中,摄像头翻转
  275. - (void)receivingCameraSwitchButtonClick {
  276. self.receivingCameraSwitchButton.selected = !self.receivingCameraSwitchButton.isSelected;
  277. [[self.trtcCloud getDeviceManager] switchCamera:self.receivingCameraSwitchButton.isSelected];
  278. }
  279. //接听中,摄像头开关
  280. - (void)receivingCameraOpenButtonClick {
  281. self.receivingCameraOpenButton.selected = !self.receivingCameraOpenButton.isSelected;
  282. self.chatingCameraOpenButton.selected = self.receivingCameraOpenButton.isSelected;
  283. if (self.receivingCameraOpenButton.isSelected) {
  284. self.receivingCameraOpenLabel.text = ASLocalizedString(@"摄像头已关");
  285. self.chatingCameraOpenLabel.text = ASLocalizedString(@"摄像头已关");
  286. [self.trtcCloud stopLocalPreview];
  287. } else {
  288. self.receivingCameraOpenLabel.text = ASLocalizedString(@"摄像头已开");
  289. self.chatingCameraOpenLabel.text = ASLocalizedString(@"摄像头已开");
  290. [self.trtcCloud startLocalPreview:YES view:self.bigPreviewView];
  291. }
  292. }
  293. //是否接听,接听
  294. - (void)receivingReceiveButtonClick {
  295. WeakSelf
  296. self.isNormalChatBlock = ^(BOOL isNormal) {
  297. if (isNormal) {
  298. [weakSelf receiveChatSendMsg];
  299. return;
  300. }
  301. [VideoChatVC destoryShareRootVC];
  302. };
  303. [self askChatIsNormal];
  304. }
  305. - (void)receiveChatSendMsg {
  306. WeakSelf
  307. SIMMsgObj *obj = [self.mChatFriend sendCustomCallVideoTextMsg:@"接听者,接听语音通话" callAudioType:AudioChatType_receive
  308. isCall:self.isZhuDongCall
  309. block:^(SResBase *resb, SIMMsgObj *newObj) {
  310. if (!resb.msuccess)
  311. {
  312. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  313. } else {
  314. //发送成功了,修改UI
  315. [weakSelf joinChannel:[NSString stringWithFormat:@"%d",self.mChatFriend.mUser_id]];
  316. weakSelf.tipLabel.hidden = YES;
  317. weakSelf.chatBotoomView.hidden = NO;
  318. weakSelf.receiveBottomView.hidden = YES;
  319. [weakSelf reloadDataWithType:(AudioChatType_receive)];
  320. [weakSelf.player stop];
  321. }
  322. }];
  323. }
  324. //是否接听,挂断通话
  325. - (void)receiveCloseButtonClick {
  326. WeakSelf
  327. SIMMsgObj *obj = [self.mChatFriend sendCustomCallVideoTextMsg:@"接听者,挂断语音通话"
  328. callAudioType:AudioChatType_beidongClose
  329. isCall:self.isZhuDongCall
  330. block:^(SResBase *resb, SIMMsgObj *newObj) {
  331. if (!resb.msuccess)
  332. {
  333. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  334. } else {
  335. //发送成功了,直接挂断销毁视图
  336. if (self.timesCount > 0) {
  337. if ([AudioChatTool.shareInstance.delegate respondsToSelector:@selector(sendTimes:)]){
  338. [AudioChatTool.shareInstance.delegate sendTimes:[NSString stringWithFormat:@"%@%@",ASLocalizedString(@"视频通话时间:"), self.timesLabel.text]];
  339. }
  340. }
  341. [VideoChatVC destoryShareRootVC];
  342. }
  343. }];
  344. }
  345. #pragma mark - 通话中点击事件
  346. - (void)maikeFengButtonClick {
  347. self.maikefengButton.selected = !self.maikefengButton.isSelected;
  348. if (self.maikefengButton.isSelected) {
  349. self.maikefengLabel.text = ASLocalizedString(@"麦克风已关闭");
  350. } else {
  351. self.maikefengLabel.text = ASLocalizedString(@"麦克风已开启");
  352. }
  353. [self setIsMute:self.maikefengButton.isSelected];
  354. }
  355. - (void)closeButtonClick {
  356. WeakSelf
  357. SIMMsgObj *obj = [self.mChatFriend sendCustomCallVideoTextMsg:@"发起者挂断视频通话" callAudioType:AudioChatType_zhudongClose
  358. isCall:self.isZhuDongCall
  359. block:^(SResBase *resb, SIMMsgObj *newObj) {
  360. if (!resb.msuccess)
  361. {
  362. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  363. } else {
  364. if (self.timesCount > 0) {
  365. if ([AudioChatTool.shareInstance.delegate respondsToSelector:@selector(sendTimes:)]){
  366. [AudioChatTool.shareInstance.delegate sendTimes:[NSString stringWithFormat:@"%@%@",ASLocalizedString(@"视频通话时间:"), self.timesLabel.text]];
  367. }
  368. }
  369. //发送成功了,直接挂断销毁视图
  370. [VideoChatVC destoryShareRootVC];
  371. }
  372. }];
  373. }
  374. - (void)yangshengqiButtonClick {
  375. self.yangshengqiButton.selected = !self.yangshengqiButton.isSelected;
  376. if (self.yangshengqiButton.isSelected) {
  377. self.yangshengqiLabel.text = ASLocalizedString(@"扬声器已开");
  378. } else {
  379. self.yangshengqiLabel.text = ASLocalizedString(@"扬声器已关");
  380. }
  381. [self openYangshengqi:self.yangshengqiButton.isSelected];
  382. }
  383. - (void)backBtnClick {
  384. //暂时直接退出,小屏先不考虑
  385. [VideoChatVC destoryShareRootVC];
  386. return;
  387. WeakSelf
  388. [[self getPresentingViewController] dismissViewControllerAnimated:YES completion:^{
  389. [weakSelf dissmissWithSmallRoom];
  390. }];
  391. }
  392. - (void)closeVoiceRoom {
  393. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  394. }];
  395. [self.trtcCloud exitRoom];
  396. [TRTCCloud destroySharedInstance];
  397. }
  398. - (UIViewController *)getPresentingViewController {
  399. NSLog(@"%s ---- %@",__FUNCTION__,self.presentingViewController);
  400. return self.presentingViewController ? self.presentingViewController : self;
  401. }
  402. #pragma mark /**最小化房间*/
  403. - (void)dissmissWithSmallRoom {
  404. // self.hasSmallRoomView = YES;
  405. // __block AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  406. // app.hasSmallRoomView = YES;
  407. // app.IsInVoiceRoom = YES;
  408. // TMSmallRoomView * smallRoomView = [TMSmallRoomView sharedInstance];
  409. // [smallRoomView updateUIWithData:self.roomDataModel.homeInfo];
  410. // [smallRoomView show];
  411. // @weakify(self)
  412. // [smallRoomView setEnterClickBlock:^{
  413. // @strongify(self)
  414. // self.hasSmallRoomView = NO;
  415. // app.showRoomVc = YES;
  416. // app.hasSmallRoomView = NO;
  417. // app.IsInVoiceRoom = YES;
  418. // UIWindow *window = [UIApplication sharedApplication].keyWindow;
  419. // UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:[TMVoiceRoomVC shareVoiceRoomVC]];
  420. // nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
  421. // nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  422. // [window.rootViewController presentViewController:nav animated:YES completion:^{}];
  423. // }];
  424. // [smallRoomView setEndClickBlock:^{
  425. // @strongify(self)
  426. // self.hasSmallRoomView = NO;
  427. // app.hasSmallRoomView = NO;
  428. // }];
  429. }
  430. #pragma mark - ******************* 通话中,数据交互,通过消息来判断逻辑进程 *******************
  431. - (void)reloadDataWithType:(AudioChatType)chatType {
  432. //1、请求语音通话,A给B发 B收到请求也要走这里判断,改变UI
  433. if (chatType == AudioChatType_call) {
  434. //发起者
  435. if (self.isZhuDongCall) {
  436. self.receiveBottomView.hidden = YES;
  437. self.chatBotoomView.hidden = NO;
  438. self.tipLabel.hidden = NO;
  439. self.tipLabel.text = ASLocalizedString(@"等待对方接受邀请...");
  440. [self startPreviewWithView:self.bigPreviewView];
  441. return;
  442. }
  443. //接收者
  444. self.receiveBottomView.hidden = NO;
  445. self.chatBotoomView.hidden = YES;
  446. self.tipLabel.hidden = NO;
  447. self.tipLabel.text = ASLocalizedString(@"邀请你视频通话");
  448. [self startPreviewWithView:self.bigPreviewView];
  449. [self playRecordWithName:@"phone_ringing" type:@"mp3" playCount:-1];
  450. return;
  451. }
  452. //2、接收者接听通话(B给A发)
  453. if (chatType == AudioChatType_receive) {
  454. self.smallPreviewView.hidden = NO;
  455. [self.trtcCloud startRemoteView:[NSString stringWithFormat:@"%d",self.mChatFriend.mUser_id] streamType:TRTCVideoStreamTypeBig view:self.smallPreviewView];
  456. if (self.isZhuDongCall) {
  457. [self joinChannel:[IMAPlatform sharedInstance].host.userId];
  458. }
  459. self.receiveBottomView.hidden = YES;
  460. self.headerImgV.hidden = YES;
  461. self.nicknameLabel.hidden = YES;
  462. self.chatBotoomView.hidden = NO;
  463. self.tipLabel.hidden = YES;
  464. self.callingCameraSwitchButton.hidden =
  465. self.callingCameraSwitchLabel.hidden = YES;
  466. self.maikefengLabel.hidden =
  467. self.maikefengButton.hidden =
  468. self.yangshengqiLabel.hidden =
  469. self.yangshengqiButton.hidden =
  470. self.chatingCameraSwitchButton.hidden = NO;
  471. //开始计时,执行计费逻辑
  472. self.timesCount = 0;
  473. [self updateTimesLabelText];
  474. self.timesLabel.hidden = NO;
  475. return;
  476. }
  477. // /** 3、挂断通话,发起者主动挂断,A给B发 */
  478. if (chatType == AudioChatType_zhudongClose) {
  479. [VideoChatVC destoryShareRootVC];
  480. [self playRecordWithName:@"close" type:@"wav" playCount:0];
  481. return;
  482. }
  483. /** 4、挂断电话,接收者挂断,B给A发 */
  484. if (chatType == AudioChatType_beidongClose) {
  485. //接收者挂断电话,直接结束通话
  486. [self playRecordWithName:@"close" type:@"wav" playCount:0];
  487. [VideoChatVC destoryShareRootVC];
  488. return;
  489. }
  490. /** 5、挂断电话,接收者忙线中,语音中或者在直播间中,B给A发 */
  491. if (chatType == AudioChatType_isBusy) {
  492. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"对方忙")];
  493. [self playRecordWithName:@"close" type:@"wav" playCount:0];
  494. //刚开始就挂断,太快了,给SDK一点反应时间,晚点再销毁,不然trtc销毁不掉
  495. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  496. [VideoChatVC destoryShareRootVC];
  497. });
  498. return;
  499. }
  500. /** 6、主动询问A是否在房间等着B通话,B给A发 */
  501. if (chatType == AudioChatType_askIsNormal) {
  502. if (self.isZhuDongCall == NO) {
  503. return;
  504. }
  505. //告诉A自己正常,正常进行通话
  506. WeakSelf
  507. SIMMsgObj *obj = [self.mChatFriend sendCustomCallVideoTextMsg:@"发起者挂断语音通话"
  508. callAudioType:AudioChatType_notificationChatNormal
  509. isCall:self.isZhuDongCall
  510. block:^(SResBase *resb, SIMMsgObj *newObj) {
  511. if (!resb.msuccess) {
  512. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  513. }
  514. }];
  515. return;
  516. }
  517. }
  518. - (void)updateTimesLabelText {
  519. if (AppDelegate.sharedAppDelegate.isInAudioVideoChatVc == NO) {
  520. return;
  521. }
  522. self.timesCount ++;
  523. int hour = self.timesCount/60/60;
  524. NSString * hoursStr = @"00";
  525. if (hour > 9) {
  526. hoursStr = [NSString stringWithFormat:@"%d",hour];
  527. } else {
  528. hoursStr = [NSString stringWithFormat:@"0%d",hour];
  529. }
  530. int min = self.timesCount/60%60;
  531. if (self.isZhuDongCall && min > self.lastMin) {
  532. [self requestPay];
  533. }
  534. self.lastMin = min;
  535. NSString * minStr = @"00";
  536. if (min > 9) {
  537. minStr = [NSString stringWithFormat:@"%d",min];
  538. } else {
  539. minStr = [NSString stringWithFormat:@"0%d",min];
  540. }
  541. int seconds = self.timesCount%60;
  542. NSString * secondsStr = @"00";
  543. if (seconds > 9) {
  544. secondsStr = [NSString stringWithFormat:@"%d",seconds];
  545. } else {
  546. secondsStr = [NSString stringWithFormat:@"0%d",seconds];
  547. }
  548. if (hour > 0) {
  549. self.timesLabel.text = [NSString stringWithFormat:@"%@:%@:%@",hoursStr,minStr,secondsStr];
  550. } else {
  551. self.timesLabel.text = [NSString stringWithFormat:@"%@:%@",minStr,secondsStr];
  552. }
  553. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  554. [self updateTimesLabelText];
  555. });
  556. }
  557. #pragma mark - 私聊付费,发起者调用 语音跟视频聊天为1分钟调用一次调用成功继续进行聊天,返回失败则直接断开,发起者进行调用
  558. - (void)requestPay {
  559. NSMutableDictionary *parmDict = [[NSMutableDictionary alloc] init];
  560. [parmDict setObject:@"user" forKey:@"ctl"];
  561. [parmDict setObject:@"chat_charging" forKey:@"act"];
  562. [parmDict setObject:@(self.mChatFriend.mUser_id) forKey:@"other_user_id"];
  563. [parmDict setObject:@"video_price" forKey:@"expend_type"];
  564. [parmDict setObject:@"0" forKey:@"i_type"];
  565. WeakSelf
  566. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  567. if ([[responseJson valueForKeyPath:@"status"]integerValue] == 1) {
  568. NSLog(@" = = = = = = = =%@",responseJson);
  569. } else if ([[responseJson valueForKeyPath:@"status"]integerValue] == 0) {
  570. //没钱或者权限不足,挂断
  571. [weakSelf closeButtonClick];
  572. }
  573. } FailureBlock:^(NSError *error) {
  574. }];
  575. }
  576. #pragma mark - ******************* 腾讯TRTC 代理方法 *******************
  577. - (void)startPreviewWithView:(UIView *)previewView{
  578. self.trtcCloud = [TRTCCloud sharedInstance];
  579. [[TRTCCloud sharedInstance] addDelegate: self];
  580. // 设置本地画面的预览模式:开启左右镜像,设置画面为填充模式
  581. TRTCRenderParams *param = [[TRTCRenderParams alloc] init];
  582. param.fillMode = TRTCVideoFillMode_Fill;
  583. param.mirrorType = TRTCVideoMirrorTypeDisable;
  584. [self.trtcCloud setLocalRenderParams:param];
  585. // 启动本地摄像头的预览(localCameraVideoView 是用于渲染本地渲染画面的控件)
  586. [self.trtcCloud startLocalPreview:YES view:previewView];
  587. // 通过 TXDeviceManager 开启自动对焦并将闪光灯打开
  588. TXDeviceManager *manager = [self.trtcCloud getDeviceManager];
  589. if ([manager isAutoFocusEnabled]) {
  590. [manager enableCameraAutoFocus:YES];
  591. }
  592. //开启闪光灯,手电筒
  593. // [manager enableCameraTorch:YES];
  594. /// 人声模式:采样率:16k;单声道;编码码率:16kbps;具备几个模式中最强的网络抗性,适合语音通话为主的场景,比如在线会议,语音通话等。
  595. [self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
  596. }
  597. - (void)joinChannel:(NSString *)room_id {
  598. TRTCParams *params = [[TRTCParams alloc] init];
  599. // 以字符串房间号为例
  600. params.roomId = room_id.intValue;
  601. params.userId = [IMAPlatform sharedInstance].host.userId;
  602. // 从业务后台获取到的 UserSig
  603. params.userSig = [IMALoginParam loadFromLocal].userSig;
  604. // 替换成您的 SDKAppID
  605. params.sdkAppId = TXSDKAppID;
  606. // 根据需要指定用户角色
  607. params.role = TRTCRoleAnchor;
  608. // 根据需要指定场景
  609. [self.trtcCloud enterRoom:params appScene:TRTCAppSceneVideoCall];
  610. TRTCVideoEncParam *videoEncParam = [[TRTCVideoEncParam alloc] init];
  611. videoEncParam.videoFps = 24;
  612. videoEncParam.resMode = TRTCVideoResolutionModePortrait;
  613. videoEncParam.videoResolution = TRTCVideoResolution_960_540;
  614. [self.trtcCloud setVideoEncoderParam:videoEncParam];
  615. }
  616. #pragma mark - 静音,默认开启
  617. - (void)setIsMute:(BOOL)isMute {
  618. if (isMute) {
  619. [self.trtcCloud muteLocalAudio:YES];
  620. return;
  621. }
  622. [self.trtcCloud muteLocalAudio:NO];
  623. }
  624. #pragma mark - 开启扬声器,默认关闭
  625. - (void)openYangshengqi:(BOOL)open {
  626. if (open) {
  627. ///使用扬声器播放(即“免提”),扬声器位于手机底部,声音偏大,适合外放音乐。
  628. [self.trtcCloud setAudioRoute:TRTCAudioModeSpeakerphone];
  629. return;
  630. }
  631. ///使用听筒播放,听筒位于手机顶部,声音偏小,适合需要保护隐私的通话场景。
  632. [self.trtcCloud setAudioRoute:TRTCAudioModeEarpiece];
  633. }
  634. // 离开房间事件回调
  635. - (void)onExitRoom:(NSInteger)reason {
  636. if (reason == 0) {
  637. NSLog(@"主动调用 exitRoom 退出房间");
  638. } else if (reason == 1) {
  639. NSLog(@"被服务器踢出当前房间");
  640. } else if (reason == 2) {
  641. NSLog(@"当前房间整个被解散");
  642. }
  643. }
  644. - (void) onEnterRoom:(NSInteger ) result {
  645. NSLog(@"onEnterRoom");
  646. if(result > 0) {
  647. //接受者进房间后,主动问一下呼叫者是否是正常状态
  648. if (self.isZhuDongCall == NO) {
  649. WeakSelf
  650. self.isNormalChatBlock = ^(BOOL isNormal) {
  651. if (isNormal == NO) {
  652. [weakSelf closeButtonClick];
  653. }
  654. };
  655. [self askChatIsNormal];
  656. }
  657. } else {
  658. // [BGHUDHelper alert:[NSString stringWithFormat:@"进入直播间直播-%ld",result]];
  659. }
  660. }
  661. - (void)askChatIsNormal {
  662. WeakSelf
  663. SIMMsgObj *obj = [self.mChatFriend sendCustomCallVideoTextMsg:@"接听者,忙线中"
  664. callAudioType:AudioChatType_askIsNormal
  665. isCall:self.isZhuDongCall
  666. block:^(SResBase *resb, SIMMsgObj *newObj) {
  667. if (!resb.msuccess)
  668. {
  669. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  670. } else {
  671. //1秒后,如果呼叫者没有回复说自己正常,那就退出房间
  672. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  673. if (weakSelf.callIsNormal && weakSelf.isNormalChatBlock) {
  674. weakSelf.isNormalChatBlock(weakSelf.callIsNormal);
  675. } else {
  676. [weakSelf judjeAskChatIsNormalBackCount:0];
  677. }
  678. });
  679. }
  680. }];
  681. }
  682. - (void)judjeAskChatIsNormalBackCount:(int)count {
  683. count ++;
  684. if (count > 10) {
  685. if (self.isNormalChatBlock) {
  686. self.isNormalChatBlock(self.callIsNormal);
  687. }
  688. return;
  689. }
  690. if (self.callIsNormal) {
  691. if (self.isNormalChatBlock) {
  692. self.isNormalChatBlock(self.callIsNormal);
  693. }
  694. return;
  695. }
  696. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  697. [self judjeAskChatIsNormalBackCount:count];
  698. });
  699. }
  700. - (void)onRemoteUserLeaveRoom:(NSString *)userId reason:(NSInteger)reason {
  701. //有人异常退出,可能不是正常退出的,自己也退出,不聊了
  702. //,直接挂断销毁视图
  703. [VideoChatVC destoryShareRootVC];
  704. NSLog(@"%@", [NSString stringWithFormat:@"用户离开%@",userId]);
  705. }
  706. //功能描述:音量大小的反馈回调
  707. - (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
  708. /*bug:当a用户在麦上正在说话时,切换到无网状态,此时虽然还显示在麦上(im还没下线),
  709. 但观众席听不到声音,光圈还在闪烁
  710. 原因: 这时观众席speakers 里面找不到a,就不会进入for循环,就不会更新a的声音为0
  711. 解决: 1先找到 麦上的用户 2去判断有无声音
  712. */
  713. // NSMutableArray<Wheat_Type_List *> *wheat_type_list = [NSMutableArray arrayWithArray:self.voiceRoomVC.roomModel.wheat_type_list];
  714. //
  715. // for (int i = 0; i < wheat_type_list.count; i ++) {
  716. //
  717. // Wheat_Type_List *info = wheat_type_list[i];
  718. //
  719. // if (info.even_wheat.user_id) {
  720. //
  721. // BOOL online = NO;
  722. //
  723. //// for (AgoraRtcAudioVolumeInfo *speaker in speakers) {
  724. // for (TRTCVolumeInfo * volumeInfo in userVolumes) {
  725. //
  726. // NSString *uid = @"";
  727. // if (volumeInfo.userId.intValue == 0) {
  728. // uid = [IMAPlatform sharedInstance].host.userId;
  729. // online = YES;
  730. // }else if (info.even_wheat.user_id == volumeInfo.userId.intValue){
  731. // uid = [NSString stringWithFormat:@"%@",volumeInfo.userId];
  732. // online = YES;
  733. // }
  734. // if (online) {
  735. // info.totalVolume = volumeInfo.volume;
  736. // // [self.users updateUser:uid volume:speaker.volume];
  737. // }
  738. // }
  739. //
  740. // wheat_type_list[i] = info;
  741. //
  742. // // if (!online) {
  743. // // [self.users updateUser:info.user_id volume:0];
  744. // // }
  745. // }
  746. // }
  747. // self.voiceRoomVC.roomModel.wheat_type_list = wheat_type_list;
  748. // [self.voiceRoomVC setRoomModel:self.voiceRoomVC.roomModel];
  749. // [self.uiController.micView setUsers:self.users];
  750. }
  751. #pragma mark - ******************* 腾讯TRTC 代理方法 end *******************
  752. #pragma mark 解决不能调为最小音量问题
  753. - (void)volumeChanged:(NSNotification *)noti
  754. {
  755. NSDictionary *tmpDict = noti.userInfo;
  756. if (tmpDict && [tmpDict isKindOfClass:[NSDictionary class]])
  757. {
  758. // if ([[tmpDict toString:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"] isEqualToString:@"ExplicitVolumeChange"] && !_isMuted)
  759. if ([[tmpDict toString:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"] isEqualToString:@"ExplicitVolumeChange"])
  760. {
  761. float volume = [[tmpDict objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
  762. if (volume <= 0.062500)
  763. {
  764. // [_publishController.txLivePublisher setMute:YES];
  765. }
  766. else
  767. {
  768. // [_publishController.txLivePublisher setMute:NO];
  769. }
  770. }
  771. }
  772. }
  773. #pragma mark - ************** 播放声音 ************
  774. /**播放录音*/
  775. -(void)playRecordWithName:(NSString *)musicName type:(NSString *)musicType playCount:(int)count {
  776. NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:musicName ofType:musicType];
  777. NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
  778. AVAudioSession *session = [AVAudioSession sharedInstance];
  779. NSError *sessionError;
  780. //AVAudioSessionCategoryPlayAndRecord,这个确保了既能录音也能播放
  781. [session setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
  782. if (session == nil) {
  783. NSLog(@"Error creating sessing:%@", [sessionError description]);
  784. } else {
  785. [session setActive:YES error:nil];
  786. }
  787. NSError *playError;
  788. self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:audioFileURL error:&playError];
  789. //无限循环
  790. self.player.numberOfLoops = count;
  791. // self.player.volume = 20;
  792. //当播放录音为空, 打印错误信息
  793. if (self.player == nil) {
  794. NSLog(@"Error crenting player: %@", [playError description]);
  795. }
  796. self.player.delegate = self;
  797. if ([self.player isPlaying]) {
  798. [self.player pause];
  799. } else {
  800. [self.player play];
  801. }
  802. }
  803. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
  804. // self.player = nil;
  805. NSLog(@"播放完成");
  806. // [self playRecord];
  807. }
  808. @end