You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
7.9 KiB

//
// TihuManager.m
// tongxin
//
// Created by ecell on 2021/7/29.
// Copyright © 2021 xTT. All rights reserved.
//
#import "TihuManager.h"
#import "BRVideoCallView.h"
#import <LSTPopView.h>
#import "UIView+XHRemoteImage.h"
#import "WHToast.h"
@interface TihuManager()<chatNetWorkStateDelegate, chatingEventDelegate, chatAVCallDelegate> {
AVAudioPlayer *audioPlayer;
}
@property (strong, nonatomic) LSTPopView *popView;
@end
static TihuManager* _manager;
static BOOL _isLogin;
@implementation TihuManager
+ (TihuManager*)shared {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
_manager = [[self alloc] init];
[MoonChatPlatform initSDKWithAppid:@"8460" appsecret:@"UkM4Q8mk3BeKEtbt"];
//[MoonChatPlatform connectServer];
//[MoonChatPlatform connectServerAddress:@"172.168.1.26" port:8083];
//后台进前台通知 UIApplicationDidBecomeActiveNotification
[[NSNotificationCenter defaultCenter] addObserver:_manager selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
//进入后台UIApplicationDidEnterBackgroundNotification
[[NSNotificationCenter defaultCenter] addObserver:_manager selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
});
return _manager;
}
-(bool)initialize {
[MoonChatPlatform getInstance].avCallDelegate = self;
[MoonChatPlatform getInstance].avChatingDelegate = self;
[MoonChatPlatform getInstance].chatNetDelegate = self;
return YES;
}
-(void)uninitialize {
}
-(void)login:(NSString *)tihuId pwd:(NSString *) pwd {
[MoonChatPlatform loginUserName:tihuId psssword:nil];
}
-(BOOL)isUserLogin {
return [MoonChatPlatform getInstance].isConnected; //_isLogin;
}
-(BOOL)queryRemoteUserOnline:(NSString *)userId callback:(UserOnlineBlock)cb {
[MoonChatPlatform checkUserOnline:userId userOnlineBlock:cb];
return NO;
}
-(void)showCallDialog:(Device *)device userId:(NSString *)dwUserId callback:(THCancelBlock)callback {
//[self playRing:@"tones"];
UINib *nib = [UINib nibWithNibName:@"BRVideoCallView" bundle:nil];
BRVideoCallView *view = [nib instantiateWithOwner:nil options:nil].firstObject;
view.nick.text = device.name;
[view.headImg setImageWithURL:[NSURL URLWithString:device.avator] placeholer:[UIImage imageNamed:@"icon_girl_head_portrait_1"]];
view.progress.hidden = NO;
[view.progress startAnimating];
view.cancelCall.hidden = NO;
view.pickUp.hidden = YES;
view.hungUp.hidden = YES;
view.layer.cornerRadius = 12;
view.layer.masksToBounds = YES;
_popView = [LSTPopView initWithCustomView:view popStyle:LSTPopStyleSmoothFromTop dismissStyle:LSTDismissStyleSmoothToBottom];
_popView.isClickBgDismiss = NO;
LSTPopViewWK(_popView)
WEAKSELF
view.cancelBlock = ^{
[wk__popView dismiss];
[weakSelf cancelCall:dwUserId];
if (callback) {
callback();
}
};
// [_popView pop];
}
//进入后台方法
- (void)didEnterBackground {
NSLog(@"===didEnterBackground===");
}
//每次后台进前台都会执行这个方法
- (void)didBecomeActive {
NSLog(@"===didBecomeActive===");
if (self.callViewController == nil && [MoonChatPlatform getInstance].curType == LBVACurrentTypeReceiced) {
//[self chatAVCallEventCallBack:MoonChatViewTypeReceiveVideo];
WEAKSELF
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf startVideoController:MoonChatViewTypeReceiveVideo isFromBg:YES];
});
}
NSLog(@"=====curType:%zd",[MoonChatPlatform getInstance].curType);
NSLog(@"=====VC:%@",self.callViewController);
}
- (void)cancelCall:(NSString *)dwUserId {
[self stopRing];
[self dismissPopView];
self.callViewController = nil;
[MoonChatPlatform closeChat];
}
- (void)dismissPopView {
if (_popView) {
[_popView dismiss];
_popView = nil;
}
}
- (void)chatNetWorkConnect:(BOOL)isConnect {
_isLogin = isConnect;
if(isConnect) {
NSLog(@"===鹈鹕用户登录成功===");
} else {
NSLog(@"===鹈鹕用户登录失败===");
}
}
//{'message': {'username': u'9224e41d8613d36d8d1da841', 'deviceName': u'\u5b9d\u8d1d', 'distinguishability_heiger': 160, 'timestamp': 1627627437.637319, 'deviceVideoType': 'TH', 'video_type': 6, 'imei': '868988040766876', 'password': '766876', 'limit_time': 300, 'appkey': 'UkM4Q8mk3BeKEtbt', 'device_username': 'imei-868988040766876', 'video_id': '868988040766876-210730144357', 'distinguishability_width': 120, 'wait_time': 60, 'openid': u'9224e41d8613d36d8d1da841', 'type': 'video'}, 'type': 'accountMessage', 'msgTitle': u'\u3010\u5b9d\u8d1d\u3011\u9080\u8bf7\u60a8\u901a\u8bdd'}
- (void)chatAVCallEventCallBack:(MoonChatViewType)avType {
// 前台才启动视频界面
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive){
[self startVideoController:avType isFromBg:NO];
} else if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
}
xLog(@"=======TihuManager chatAVCallEventCallBack========");
}
- (void)startVideoController:(MoonChatViewType)avType isFromBg:(BOOL)bg
{
TihuVideoCallViewController *vc = [TihuVideoCallViewController new];
vc.device = self.device;
vc.vType = avType;
vc.iRemoteUserId = _thModel.deviceName;//;
vc.videoId = _thModel.video_id;
vc.hidesBottomBarWhenPushed = YES;
vc.isFromBackground = bg;
self.callViewController = vc;
[MoonChatPlatform getInstance].avChatingDelegate = vc;
if (![[self.gMianVC.navigationController.viewControllers lastObject] isKindOfClass:vc.class]) {
UIViewController *topVC = [self getCurrentVC];
[topVC.navigationController pushViewController:vc animated:YES];
}
}
// MARK: 获取顶部视图 add by lsz 2021-06-19
- (UIViewController *)getCurrentVC
{
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
return currentVC;
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
// 视图是被presented出来的
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
// 根视图为UITabBarController
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]){
// 根视图为UINavigationController
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
// 根视图为非导航类
currentVC = rootVC;
}
return currentVC;
}
- (void)chatAVCloseEventCallBack:(int)type {
xLog(@"1.0 chatAVCloseEventCallBack===%d", type);
}
- (void)chatAudioInit:(BOOL)isCalling {
}
- (void)chatVideoSessionInit:(nonnull AVCaptureSession *)session {
}
- (void)chatVideoDataReceived {
}
- (void)chatVideoPreviewInit:(nonnull AVCaptureSession *)session {
}
//MARK: 响铃的 MP3
- (void)playRing:(NSString *)name {
[self stopRing];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
NSURL *path = [[NSBundle mainBundle] URLForResource:name withExtension:@"mp3"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
- (void)stopRing {
if (audioPlayer) {
[audioPlayer stop];
audioPlayer = nil;
}
}
@end