iOS - 自主实现类似微信语音视频信息聊天 (idoubs详细使用方法)1.0

来源:互联网 发布:高清截图软件 编辑:程序博客网 时间:2024/04/28 21:21

2017 年 本命年到了 一定要兢兢业业 努力去让自己变得更好~ 先给大家拜个年啦。
这篇文章早在16年底就已经在pages上码了一遍 因为总有事情 就一直没有放上来。

好了 话不多说 今天主要不是讲如何配置doubango 之前的文章已经提过 链接在这里:
iOS - 工程引入doubango (idoubs编译)

也不是教大家怎么判断对方来电挂机拒接等操作,之前也写过了,链接在这里:
iOS - idoubs 通话判断对方状态(在线、拒接、无人接听、挂断)

最后,在放上本人自主写的一个DemoApp , github地址是:
ZYDoubs下载地址

随意展示一下页面(还在完善中 没有完全做完 但是可以先关注呀~)
通话界面

================================================================================

注意听讲,下面进入正题

一、引擎:

1、相关类:NgnEngine

2、使用说明:

引擎类具有8个服务,分别为:协议服务、配置服务、联系人服务、http连接服务、历史服务、声音服务、网络服务、存储服务。其中最为重要的是协议服务,里面有协议栈,用来回调(内部已处理)。
NgnBaseService<INgnSipService>* mSipService;NgnBaseService<INgnConfigurationService>* mConfigurationService;NgnBaseService<INgnContactService>* mContactService;NgnBaseService<INgnHttpClientService>* mHttpClientService;NgnBaseService<INgnHistoryService>* mHistoryService;NgnBaseService<INgnSoundService>* mSoundService;NgnBaseService<INgnNetworkService>* mNetworkService;NgnBaseService<INgnStorageService>* mStorageService;

3、详细说明:

①获取引擎:
idoubs整体逻辑是只用一个引擎。为了能跑通程序,通常用以下代码获取全局引擎。

[NgnEngine sharedInstance]

②打开服务:会把2中的服务打开。

[[NgnEngine sharedInstance] start];

③停止服务:

[[NgnEngine sharedInstance] stop];

④程序处于后台时,可以使用startKeepAwake方法保持唤醒状态。

[[NgnEngine sharedInstance] startKeepAwake];

⑤相对应,结束保持唤醒状态。

[[NgnEngine sharedInstance] stopKeepAwake];

二、配置服务:

1、相关类: NgnConfigurationService

2、使用说明:

设置昵称:key字段 IDENTITY_DISPLAY_NAME设置impi :   key字段 IDENTITY_IMPI设置impu:    key字段 IDENTITY_IMPU设置sip密码:key字段 IDENTITY_PASSWORD设置域:key字段 NETWORK_REALM设置代理主机: key字段 NETWORK_PCSCF_HOST设置代理主机端口: key字段 NETWORK_PCSCF_PORT设置是否允许3G网络: key字段 NETWORK_USE_3G设置是否允许3GPP Early IMS: key字段 NETWORK_USE_EARLY_IMS设置是否允许后台保持清醒: key字段 NETWORK_USE_KEEPAWAKE

3、具体使用:

UserManager * user = [UserManagerTool userManager];    NSString * kPublicIdentity = [NSString stringWithFormat:@"sip:%@@%@",user.user_sip,user.sip_host_addr];    // set credentials    [[NgnEngine sharedInstance].configurationService setStringWithKey:IDENTITY_DISPLAY_NAME andValue:user.worker_name];    [[NgnEngine sharedInstance].configurationService setStringWithKey:IDENTITY_IMPI andValue:user.user_sip];    [[NgnEngine sharedInstance].configurationService setStringWithKey:IDENTITY_IMPU andValue:kPublicIdentity];    [[NgnEngine sharedInstance].configurationService setStringWithKey:IDENTITY_PASSWORD andValue:user.user_password];    [[NgnEngine sharedInstance].configurationService setStringWithKey:NETWORK_REALM andValue:user.sip_host_addr];    [[NgnEngine sharedInstance].configurationService setStringWithKey:NETWORK_PCSCF_HOST andValue:user.sip_host_addr];    int intPort = [user.sip_host_port intValue];    [[NgnEngine sharedInstance].configurationService setIntWithKey:NETWORK_PCSCF_PORT andValue:intPort];    [[NgnEngine sharedInstance].configurationService setBoolWithKey:NETWORK_USE_EARLY_IMS andValue:YES];    [[NgnEngine sharedInstance].configurationService setBoolWithKey:NETWORK_USE_3G andValue:YES];    [[NgnEngine sharedInstance].configurationService setBoolWithKey:NETWORK_USE_KEEPAWAKE andValue:YES];    SYLog(@" 配置   %@ \n%@\n %@\n",user.user_sip,user.user_password,kPublicIdentity);

三、协议服务:

1、相关类: NgnSipService

2、使用说明:

①注册(可以理解为登录)
内部实现开启一个新的协议栈用来回调事件状态等。
注意:必须先配置好再注册

[[NgnEngine sharedInstance].sipService registerIdentity];

②注销:
内部实现 销毁协议栈,停止服务。

[[NgnEngine sharedInstance].sipService unRegisterIdentity];

③判断是否注册: 返回BOOL类型

[[NgnEngine sharedInstance].sipService isRegistered];

四、拨打中转站

1、相关类:CallViewController

2、CallViewController 相当于一个中转站,拨打和接收都要通过它

①拨打语音
参数1: 对方的sip账号的impi 参数2:协议栈

NSString * sipNum = [NSString stringWithFormat:@"%@",self.contactModel.user_sip];[CallViewController makeAudioCallWithRemoteParty:sipNum andSipStack:[[NgnEngine sharedInstance].sipService getSipStack]];

②拨打视频
参数1: 对方的sip账号的impi 参数2:协议栈

NSString * sipNum = [NSString stringWithFormat:@"%@",self.contactModel.user_sip];[CallViewController makeAudioVideoCallWithRemoteParty:sipNum  andSipStack: [[NgnEngine sharedInstance].sipService getSipStack]];

③语音视频呼入,一般是由本地通知发起,通知的notification.userInfo 里面会保存类型,
用来区别是语音/视频来电 还是 信息来了。
语音/视频 : kNotifKey_IncomingCall 信息:kNotifKey_IncomingMsg

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    NSLog(@"notification ==== %@",notification);    NSString *notifKey = [notification.userInfo objectForKey:kNotifKey];    if([notifKey isEqualToString:kNotifKey_IncomingCall]){        NSNumber* sessionId = [notification.userInfo objectForKey:kNotifIncomingCall_SessionId];        NgnAVSession* session = [NgnAVSession getSessionWithId:[sessionId longValue]];        if(session){            [CallViewController receiveIncomingCall:session];            application.applicationIconBadgeNumber -= notification.applicationIconBadgeNumber;        }    }    else if([notifKey isEqualToString:kNotifKey_IncomingMsg]){        UserManager * user = [UserManagerTool userManager];        NSString * userName = [notification.userInfo objectForKey:@"userName"];        NSString * myClientId = clientID;        NSString * showStr = [notification.userInfo objectForKey:@"content"];        myClientId = [NSString stringWithFormat:@"|=|%@|=|",myClientId];        if ([userName isEqualToString:user.user_sip]) {            if (![myClientId isEqualToString: showStr]) {                //id账号不同踢下线                [PMSipTools sipUnRegister];                [self alertLoginOtherWhere];                return;            }        }        if (![self.window.rootViewController isKindOfClass:[MyRootViewController class]]) {            return;        }        MyRootViewController * rootVC = (MyRootViewController *)self.window.rootViewController;        UINavigationController * nav = rootVC.midViewController;        [nav popToRootViewControllerAnimated:NO];        //跳去聊天界面        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushChatVC" object:userName];    }}

五、语音拨打界面

1、相关类:AudioCallViewController

2、使用:

①注册通知:用来获取拨打电话的状态,通过状态来判断“通话中或已挂断等”

-(void) onInviteEvent:(NSNotification*)notification {    NgnInviteEventArgs* eargs = [notification object];    if(!audioSession || audioSession.id != eargs.sessionId){        return;    }      switch (eargs.eventType) {        case INVITE_EVENT_INPROGRESS:        case INVITE_EVENT_INCOMING:        case INVITE_EVENT_RINGING:        case INVITE_EVENT_LOCAL_HOLD_OK:        case INVITE_EVENT_REMOTE_HOLD:        default:        {            // updates view and state            [self updateViewAndState];            break;        }            // transilient events        case INVITE_EVENT_MEDIA_UPDATING:        {            self.labelStatus.text = @"语音来电..";            break;        }        case INVITE_EVENT_MEDIA_UPDATED:        {            self.labelStatus.text = @"语音结束中..";            break;        }        case INVITE_EVENT_TERMINATED:        case INVITE_EVENT_TERMWAIT:        {            // updates view and state            [self updateViewAndState];            // releases session            [NgnAVSession releaseSession: &audioSession];            // starts timer suicide            [NSTimer scheduledTimerWithTimeInterval: kCallTimerSuicide                                             target: self                                           selector: @selector(timerSuicideTick:)                                           userInfo: nil                                            repeats: NO];            break;        }    }}

③挂断 接受 免提 静音:

//挂断- (void) buttonHangupClick{    if(audioSession){        [audioSession hangUpCall];    }}//接受 - (void) buttonAcceptClick{    if(audioSession){        [audioSession acceptCall];    }}//免提- (void)handsFreeBtnClick:(UIButton *)sender {    [audioSession setSpeakerEnabled:![audioSession isSpeakerEnabled]];    if([[NgnEngine sharedInstance].soundService setSpeakerEnabled:[audioSession isSpeakerEnabled]]){        self.handsFreeBtn.selected = [audioSession isSpeakerEnabled];    }}//静音- (void)muteBtnClick:(UIButton *)sender {    if([audioSession setMute:![audioSession isMuted]]){        self.muteBtn.selected = [audioSession isMuted];    }}

④ 播放来电铃声:
来电外放:

[[[NgnEngine sharedInstance] getSoundService] playBackRingTone];

来电不外放(听筒内可以听到)

[[[NgnEngine sharedInstance] getSoundService] playRingTone];

停止播放(在设置了来电免打扰的时候使用)

[[[NgnEngine sharedInstance] getSoundService] stopRingTone];[[[NgnEngine sharedInstance] getSoundService] stopRingBackTone];

六、视频拨打界面

1、相关类:VideoCallViewController

2、使用方法:

                大部分同语言拨打界面,在此不重复叙述!

未完待续,感谢你的耐心!
有疑问可以留言呀,虽然看的人不多 - -,
需要再详解哪部分留言或私信,我会整合放在下一篇文章里!

0 0
原创粉丝点击