AskCallViewController.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // AskCallViewController.m
  3. // shiku_im
  4. //
  5. // Created by MacZ on 2017/8/7.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "AskCallViewController.h"
  9. #import "JXCustomButton.h"
  10. #ifdef Live_Version
  11. #import "JXLiveJidManager.h"
  12. #endif
  13. #import "JXAVCallViewController.h"
  14. @interface AskCallViewController ()
  15. @property (strong, nonatomic) UIView *viewTop;
  16. @property (strong, nonatomic) UIImageView *headerImage;
  17. @property (strong, nonatomic) UILabel *labelStatus;
  18. @property (strong, nonatomic) UILabel *labelRemoteParty;
  19. @property (strong, nonatomic) UIView *viewCenter;
  20. @property (strong, nonatomic) UIImageView *imageSecure;
  21. @property (nonatomic, strong) UIImageView *animationImgV;
  22. @property (strong, nonatomic) UIView *viewBottom;
  23. @property (strong, nonatomic) UIButton *buttonHangup;
  24. @property (nonatomic, strong) UILabel *timeLabel;
  25. @property (nonatomic, assign) int timerNum;
  26. @end
  27. #define Button_Width 63
  28. #define Button_Height 63
  29. //#define BtnImage_big 56
  30. //#define BtnImage_small 34
  31. #define BtnImage_big 63
  32. #define BtnImage_small 34
  33. @implementation AskCallViewController
  34. - (id)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. self.isGotoBack = YES;
  39. self.heightHeader = 0;
  40. self.heightFooter = 0;
  41. self.view.frame = g_window.bounds;
  42. [self createHeadAndFoot];
  43. g_meeting.isMeeting = YES;
  44. [self customView];
  45. [g_notify addObserver:self selector:@selector(newMsgCome:) name:kXMPPNewMsgNotifaction object:nil];
  46. [g_notify addObserver:self selector:@selector(onSendTimeout:) name:kXMPPSendTimeOutNotifaction object:nil];
  47. [g_meeting sendAsk:self.type toUserId:self.toUserId toUserName:self.toUserName meetUrl:self.meetUrl];
  48. _bAnswer = NO;
  49. //[self performSelector:@selector(checkAnswer) withObject:nil afterDelay:30];
  50. // [self performSelector:@selector(doCall) withObject:nil afterDelay:10];
  51. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:self.toUserId];
  52. if ([user.offlineNoPushMsg intValue] != 1) {
  53. _player = [[JXAudioPlayer alloc]init];
  54. _player.isOpenProximityMonitoring = NO;
  55. _player.audioFile = [NSString stringWithFormat:@"%@Ring.mp3",imageFilePath];
  56. [_player open];
  57. [_player play];
  58. _player.player.numberOfLoops = 10000;
  59. }
  60. }
  61. return self;
  62. }
  63. - (void) customView {
  64. self.tableBody.backgroundColor = [UIColor whiteColor];
  65. _labelRemoteParty = [[UILabel alloc] init];
  66. _labelRemoteParty.frame = CGRectMake(0, 109, JX_SCREEN_WIDTH, 25);
  67. _labelRemoteParty.textColor = HEXCOLOR(0x333333);
  68. _labelRemoteParty.font = [UIFont systemFontOfSize:24];
  69. _labelRemoteParty.textAlignment = NSTextAlignmentCenter;
  70. _labelRemoteParty.text = self.toUserName;
  71. [self.tableBody addSubview:_labelRemoteParty];
  72. _labelStatus = [[UILabel alloc] init];
  73. _labelStatus.frame = CGRectMake(0, CGRectGetMaxY(_labelRemoteParty.frame) + 20, JX_SCREEN_WIDTH, 18);
  74. _labelStatus.textColor = HEXCOLOR(0x333333);
  75. _labelStatus.font = [UIFont systemFontOfSize:17];
  76. _labelStatus.textAlignment = NSTextAlignmentCenter;
  77. [self.tableBody addSubview:_labelStatus];
  78. NSString *str;
  79. if (self.type == kWCMessageTypeAudioChatAsk) {
  80. str = @"等待语音接听...";
  81. }else if (self.type == kWCMessageTypeVideoChatAsk) {
  82. str = @"等待视频接听...";
  83. }
  84. _labelStatus.text = str;
  85. NSMutableArray *images = [[NSMutableArray alloc] init];
  86. for (int i = 1; i <= 3; i ++) {
  87. UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Talk_Animation_%d",i]];
  88. [images addObject:img];
  89. }
  90. _animationImgV = [[UIImageView alloc] initWithFrame:CGRectMake((JX_SCREEN_WIDTH-145)/2, (JX_SCREEN_HEIGHT-145)/2-30, 145, 145)];
  91. _animationImgV.animationImages = images;
  92. _animationImgV.animationDuration = 1.5f;
  93. [self.tableBody addSubview:_animationImgV];
  94. [_animationImgV startAnimating];
  95. _headerImage = [[UIImageView alloc] init];
  96. _headerImage.frame = CGRectMake((_animationImgV.frame.size.width-100)/2, (_animationImgV.frame.size.height-100)/2, 100, 100);
  97. _headerImage.userInteractionEnabled = YES;
  98. _headerImage.layer.cornerRadius = _headerImage.frame.size.width / 2;
  99. _headerImage.layer.masksToBounds = YES;
  100. [g_server getHeadImageLarge:self.toUserId userName:self.toUserName imageView:_headerImage];
  101. [_animationImgV addSubview:_headerImage];
  102. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_animationImgV.frame) + 20, JX_SCREEN_WIDTH, 20)];
  103. _timeLabel.font = SYSFONT(16);
  104. _timeLabel.textColor = HEXCOLOR(0x333333);
  105. _timeLabel.textAlignment = NSTextAlignmentCenter;
  106. _timeLabel.text = [TimeUtil getTimeShort:_timerNum];
  107. [self.tableBody addSubview:_timeLabel];
  108. //viewHeader viewTop
  109. // _viewTop = [[UIView alloc] init];
  110. // _viewTop.frame = CGRectMake(0, 40, JX_SCREEN_WIDTH, 86);
  111. // _viewTop.userInteractionEnabled = YES;
  112. // _viewTop.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2 - 100);
  113. // [self.tableBody addSubview:_viewTop];
  114. //viewFooter viewBottom
  115. // _viewBottom = [[UIView alloc] init];
  116. // _viewBottom.frame = CGRectMake(0, JX_SCREEN_HEIGHT*3.2/5, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT/2);
  117. // _viewBottom.userInteractionEnabled = YES;
  118. // [self.tableBody addSubview:_viewBottom];
  119. _buttonHangup = [self createBottomButtonWithImage:@"hang_up" SelectedImg:nil selector:@selector(doCancel) btnWidth:Button_Width imageWidth:BtnImage_big];
  120. [_buttonHangup setTitle:Localized(@"JXMeeting_Hangup") forState:UIControlStateNormal];
  121. _buttonHangup.frame = CGRectMake((JX_SCREEN_WIDTH - Button_Width) / 2 , CGRectGetMaxY(_timeLabel.frame)+(JX_SCREEN_HEIGHT-CGRectGetMaxY(_timeLabel.frame)-Button_Height)/2-20, Button_Width, Button_Height);
  122. self.tableBody.contentSize = CGSizeMake(0, 0);
  123. }
  124. - (void)viewDidLoad {
  125. [super viewDidLoad];
  126. _timerNum = 0;
  127. _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  128. }
  129. // 30秒无响应 自动挂断
  130. - (void)timerAction:(NSTimer *)timer {
  131. _timerNum ++;
  132. NSLog(@"timerNum = %d", _timerNum);
  133. if (_timerNum > 32) {
  134. [_animationImgV stopAnimating];
  135. [timer invalidate];
  136. timer = nil;
  137. _timerNum = 0;
  138. [self doCancel];
  139. }
  140. _timeLabel.text = [TimeUtil getTimeShort:_timerNum];
  141. }
  142. -(JXCustomButton *)createBottomButtonWithImage:(NSString *)Image SelectedImg:(NSString *)selectedImage selector:(SEL)selector btnWidth:(CGFloat)btnWidth imageWidth:(CGFloat)imageWidth{
  143. JXCustomButton * button = [JXCustomButton buttonWithType:UIButtonTypeCustom];
  144. [button setImage:[UIImage imageNamed:Image] forState:UIControlStateNormal];
  145. [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
  146. [button.titleLabel setFont:g_factory.font12];
  147. [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
  148. button.titleRect = CGRectMake(0, imageWidth+(btnWidth-imageWidth)/2, btnWidth, 20);
  149. button.imageRect = CGRectMake((btnWidth-imageWidth)/2, 0, imageWidth, imageWidth);
  150. if (selector)
  151. [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
  152. [self.tableBody addSubview:button];
  153. return button;
  154. }
  155. -(void)dealloc{
  156. //移除监听
  157. [g_notify removeObserver:self];
  158. }
  159. - (void)didReceiveMemoryWarning {
  160. [super didReceiveMemoryWarning];
  161. // Dispose of any resources that can be recreated.
  162. }
  163. #pragma mark 接受新消息广播
  164. -(void)newMsgCome:(NSNotification *)notifacation{
  165. JXMessageObject *msg = (JXMessageObject *)notifacation.object;
  166. if(msg==nil)
  167. return;
  168. if ([msg.type integerValue] == kWCMessageTypeAVBusy) {
  169. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  170. g_meeting.isMeeting = NO;
  171. [g_App endCall];
  172. [self actionQuit];
  173. [g_server showMsg:Localized(@"JX_TheOtherBusy")];
  174. });
  175. return;
  176. }
  177. #ifdef Live_Version
  178. if([[JXLiveJidManager shareArray] contains:msg.toUserId] || [[JXLiveJidManager shareArray] contains:msg.fromUserId])
  179. return;
  180. #endif
  181. if([msg.type intValue] == kWCMessageTypeAudioChatAccept){
  182. JXAVCallViewController *avVC = [[JXAVCallViewController alloc] init];
  183. avVC.isAudio = YES;
  184. avVC.isGroup = NO;
  185. avVC.toUserId = msg.fromUserId;
  186. avVC.toUserName = msg.fromUserName;
  187. avVC.meetUrl = self.meetUrl;
  188. avVC.roomNum = MY_USER_ID;
  189. avVC.view.frame = [UIScreen mainScreen].bounds;
  190. [g_window addSubview:avVC.view];
  191. [self actionQuit];
  192. // UIViewController *lastVC = (UIViewController *)g_navigation.lastVC;
  193. // [lastVC presentViewController:avVC animated:NO completion:nil];
  194. }else if ([msg.type intValue] == kWCMessageTypeVideoChatAccept) {
  195. JXAVCallViewController *avVC = [[JXAVCallViewController alloc] init];
  196. avVC.isAudio = NO;
  197. avVC.isGroup = NO;
  198. avVC.toUserId = msg.fromUserId;
  199. avVC.toUserName = msg.fromUserName;
  200. avVC.meetUrl = self.meetUrl;
  201. avVC.roomNum = MY_USER_ID;
  202. avVC.view.frame = [UIScreen mainScreen].bounds;
  203. [g_window addSubview:avVC.view];
  204. [self actionQuit];
  205. // UIViewController *lastVC = (UIViewController *)g_navigation.lastVC;
  206. // [lastVC presentViewController:avVC animated:NO completion:nil];
  207. }
  208. if ([msg.type intValue] == kWCMessageTypeAudioChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatCancel) {
  209. g_meeting.isMeeting = NO;
  210. [self actionQuit];
  211. [g_App endCall];
  212. }
  213. // 多点登录
  214. if ([msg.fromUserId isEqualToString:MY_USER_ID]) {
  215. if ([msg.type intValue] == kWCMessageTypeAudioChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatEnd || [msg.type intValue] == kWCMessageTypeAudioChatEnd) {
  216. [_player stop];
  217. _player = nil;
  218. g_meeting.isMeeting = NO;
  219. [self actionQuit];
  220. [g_App endCall];
  221. }
  222. }
  223. // if([msg.type intValue] == kWCMessageTypeAudioChatReady || [msg.type intValue] == kWCMessageTypeVideoChatReady){
  224. // if([msg.fromUserId isEqualToString:self.toUserId]){
  225. // [self doCall];
  226. // }
  227. // }
  228. }
  229. -(void)onSendTimeout:(NSNotification *)notifacation//超时未收到回执
  230. {
  231. JXMessageObject *msg = (JXMessageObject *)notifacation.object;
  232. if(msg==nil)
  233. return;
  234. if([msg.type intValue] == kWCMessageTypeAudioChatAsk || [msg.type intValue] == kWCMessageTypeVideoChatAsk){
  235. // [g_App showAlert:@"网络不好,无法送达"];
  236. [self doNoAnswer:[msg.type intValue]];
  237. return;
  238. }
  239. }
  240. -(void)doCancel{
  241. _bAnswer = YES;
  242. int n;
  243. if(self.type == kWCMessageTypeAudioChatAsk)
  244. n = kWCMessageTypeAudioChatCancel;
  245. else
  246. n = kWCMessageTypeVideoChatCancel;
  247. [g_meeting sendCancel:n toUserId:self.toUserId toUserName:self.toUserName];
  248. g_meeting.isMeeting = NO;
  249. [g_App endCall];
  250. [self actionQuit];
  251. }
  252. -(void)checkAnswer{
  253. if(!_bAnswer)
  254. [self doNoAnswer:self.type];
  255. }
  256. -(void)doNoAnswer:(int)type{
  257. int n;
  258. if(type == kWCMessageTypeAudioChatAsk)
  259. n = kWCMessageTypeAudioChatCancel;
  260. else
  261. n = kWCMessageTypeVideoChatCancel;
  262. [g_meeting sendNoAnswer:n toUserId:self.toUserId toUserName:self.toUserName];
  263. g_meeting.isMeeting = NO;
  264. [g_App endCall];
  265. [self actionQuit];
  266. }
  267. -(void)actionQuit{
  268. [_player stop];
  269. _player = nil;
  270. [_timer invalidate];
  271. _timer = nil;
  272. [super actionQuit];
  273. }
  274. @end