美文网首页iOS开发技能
简单集成环信视频通话

简单集成环信视频通话

作者: 前端杜若 | 来源:发表于2016-08-05 17:20 被阅读2160次

之前在弄环信的视频通话,找了好久都没有找到资源,尤其是简书上面,虽然环信官方有集成音视频的视频,但是那个是在太模糊了,我这里只能被动接收视频,下面开始吧
我用的SDK是这个ios_IM_sdk_V3.1.4
1.将HyphenateFullSDK导入项目中然后

class.png

这些环信官方文档都有滴
2.登录注册的接口加入项目中
重点来了
3
ChatDemoHelper.h 我进行了删减

/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import <Foundation/Foundation.h>

//#import "ConversationListController.h"
//#import "ContactListViewController.h"
#import "ViewController.h"
//#import "ChatViewController.h"

#define kHaveUnreadAtMessage    @"kHaveAtMessage"
#define kAtYouMessage           1
#define kAtAllMessage           2

#if DEMO_CALL == 1

#import "CallViewController.h"

@interface ChatDemoHelper : NSObject <EMClientDelegate,EMChatManagerDelegate,EMContactManagerDelegate,EMGroupManagerDelegate,EMChatroomManagerDelegate,EMCallManagerDelegate>

#else

@interface ChatDemoHelper : NSObject <EMClientDelegate,EMChatManagerDelegate,EMContactManagerDelegate,EMGroupManagerDelegate,EMChatroomManagerDelegate>

#endif
//
//@property (nonatomic, weak) ContactListViewController *contactViewVC;
//
//@property (nonatomic, weak) ConversationListController *conversationListVC;

@property (nonatomic, weak) ViewController  *mainVC;

//@property (nonatomic, weak) ChatViewController *chatVC;

#if DEMO_CALL == 1

@property (strong, nonatomic) EMCallSession *callSession;
@property (strong, nonatomic) CallViewController *callController;

#endif

+ (instancetype)shareHelper;


#if DEMO_CALL == 1

- (void)makeCallWithUsername:(NSString *)aUsername
                     isVideo:(BOOL)aIsVideo;

- (void)hangupCallWithReason:(EMCallEndReason)aReason;

- (void)answerCall;

#endif

@end

ChatDemoHelper.m

/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "ChatDemoHelper.h"

#import "AppDelegate.h"
#if DEMO_CALL == 1

#import "CallViewController.h"

@interface ChatDemoHelper()<EMCallManagerDelegate>
{
    NSTimer *_callTimer;
}

@end

#endif

static ChatDemoHelper *helper = nil;

@implementation ChatDemoHelper

+ (instancetype)shareHelper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[ChatDemoHelper alloc] init];
    });
    return helper;
}

- (void)dealloc
{
    [[EMClient sharedClient] removeDelegate:self];
    [[EMClient sharedClient].groupManager removeDelegate:self];
    [[EMClient sharedClient].contactManager removeDelegate:self];
    [[EMClient sharedClient].roomManager removeDelegate:self];
    [[EMClient sharedClient].chatManager removeDelegate:self];
    
#if DEMO_CALL == 1
    [[EMClient sharedClient].callManager removeDelegate:self];
#endif
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initHelper];
    }
    return self;
}

- (void)initHelper
{
#ifdef REDPACKET_AVALABLE
    [[RedPacketUserConfig sharedConfig] beginObserveMessage];
#endif
    
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    
#if DEMO_CALL == 1
    [[EMClient sharedClient].callManager addDelegate:self delegateQueue:nil];
    
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeCall:) name:KNOTIFICATION_CALL object:nil];
#endif
}

- (void)asyncPushOptions
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        EMError *error = nil;
        [[EMClient sharedClient] getPushOptionsFromServerWithError:&error];
    });
}


#pragma mark - EMCallManagerDelegate

#if DEMO_CALL == 1

- (void)didReceiveCallIncoming:(EMCallSession *)aSession
{
    
    if(_callSession && _callSession.status != EMCallSessionStatusDisconnected){
        [[EMClient sharedClient].callManager endCall:aSession.sessionId reason:EMCallEndReasonBusy];
    }
    
    if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
        [[EMClient sharedClient].callManager endCall:aSession.sessionId reason:EMCallEndReasonFailed];
    }
    
    _callSession = aSession;
    if(_callSession){
        [self _startCallTimer];
        
        _callController = [[CallViewController alloc] initWithSession:_callSession isCaller:NO status:@"连接建立完成"];
        _callController.modalPresentationStyle = UIModalPresentationOverFullScreen;
       // _mainVC = [[MainViewController alloc]init];
        [_mainVC presentViewController:_callController animated:NO completion:nil];
    }
}

- (void)didReceiveCallConnected:(EMCallSession *)aSession
{
    if ([aSession.sessionId isEqualToString:_callSession.sessionId]) {
        _callController.statusLabel.text = @"连接建立完成";
        
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [audioSession setActive:YES error:nil];
    }
}

- (void)didReceiveCallAccepted:(EMCallSession *)aSession
{
    if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
        [[EMClient sharedClient].callManager endCall:aSession.sessionId reason:EMCallEndReasonFailed];
    }
    
    if ([aSession.sessionId isEqualToString:_callSession.sessionId]) {
        [self _stopCallTimer];
        
        NSString *connectStr = aSession.connectType == EMCallConnectTypeRelay ? @"Relay" : @"Direct";
        _callController.statusLabel.text = [NSString stringWithFormat:@"%@ %@",@"可以说话了...", connectStr];
        _callController.timeLabel.hidden = NO;
        [_callController startTimer];
        [_callController startShowInfo];
        _callController.cancelButton.hidden = NO;
        _callController.rejectButton.hidden = YES;
        _callController.answerButton.hidden = YES;
    }
}

- (void)didReceiveCallTerminated:(EMCallSession *)aSession
                          reason:(EMCallEndReason)aReason
                           error:(EMError *)aError
{
    if ([aSession.sessionId isEqualToString:_callSession.sessionId]) {
        [self _stopCallTimer];
        
        _callSession = nil;
        
        [_callController close];
        _callController = nil;
        
        if (aReason != EMCallEndReasonHangup) {
            NSString *reasonStr = @"";
            switch (aReason) {
                case EMCallEndReasonNoResponse:
                {
                    reasonStr = @"对方没有回应";
                }
                    break;
                case EMCallEndReasonDecline:
                {
                    reasonStr = @"拒接通话";
                }
                    break;
                case EMCallEndReasonBusy:
                {
                    reasonStr = @"正在通话中...";
                }
                    break;
                case EMCallEndReasonFailed:
                {
                    reasonStr = @"建立连接失败";
                }
                    break;
                default:
                    break;
            }
            
            if (aError) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:aError.errorDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:reasonStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
        }
    }
}

- (void)didReceiveCallNetworkChanged:(EMCallSession *)aSession status:(EMCallNetworkStatus)aStatus
{
    if ([aSession.sessionId isEqualToString:_callSession.sessionId]) {
        [_callController setNetwork:aStatus];
    }
}

#endif

#pragma mark - public 

#if DEMO_CALL == 1

- (void)makeCall:(NSNotification*)notify
{
    if (notify.object) {
        [self makeCallWithUsername:[notify.object valueForKey:@"chatter"] isVideo:[[notify.object objectForKey:@"type"] boolValue]];
    }
}

- (void)_startCallTimer
{
    _callTimer = [NSTimer scheduledTimerWithTimeInterval:50 target:self selector:@selector(_cancelCall) userInfo:nil repeats:NO];
}

- (void)_stopCallTimer
{
    if (_callTimer == nil) {
        return;
    }
    
    [_callTimer invalidate];
    _callTimer = nil;
}

- (void)_cancelCall
{
    [self hangupCallWithReason:EMCallEndReasonNoResponse];
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"没有响应,自动挂断" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

- (void)makeCallWithUsername:(NSString *)aUsername
                     isVideo:(BOOL)aIsVideo
{
    if ([aUsername length] == 0) {
        return;
    }
    //是视频
    if (aIsVideo) {
        _callSession = [[EMClient sharedClient].callManager makeVideoCall:aUsername error:nil];
    }
    else{
        _callSession = [[EMClient sharedClient].callManager makeVoiceCall:aUsername error:nil];
    }
    
    if(_callSession){
        [self _startCallTimer];
        
        _callController = [[CallViewController alloc] initWithSession:_callSession isCaller:YES status:@"正在建立连接..."];
        [_mainVC presentViewController:_callController animated:NO completion:nil];
    }
    else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"创建实时通话失败,请稍后重试" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
}

- (void)hangupCallWithReason:(EMCallEndReason)aReason
{
    [self _stopCallTimer];
    
    if (_callSession) {
        [[EMClient sharedClient].callManager endCall:_callSession.sessionId reason:aReason];
    }
    
    _callSession = nil;
    [_callController close];
    _callController = nil;
}

- (void)answerCall
{
    if (_callSession) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            EMError *error = [[EMClient sharedClient].callManager answerIncomingCall:_callSession.sessionId];
            if (error) {
                NSLog(@"错了");
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (error.code == EMErrorNetworkUnavailable) {
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络连接失败" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
                        [alertView show];
                    }
                    else{
                        NSLog(@"挂断");
                        [self hangupCallWithReason:EMCallEndReasonFailed];
                    }
                });
            }
        });
    }
}

#endif

#pragma mark - private
- (BOOL)_needShowNotification:(NSString *)fromChatter
{
    BOOL ret = YES;
    NSArray *igGroupIds = [[EMClient sharedClient].groupManager getAllIgnoredGroupIds];
    for (NSString *str in igGroupIds) {
        if ([str isEqualToString:fromChatter]) {
            ret = NO;
            break;
        }
    }
    return ret;
}

@end

这里需要注意一点,ViewController.h 是根据你的主视图控制器来进行更改,环信上面是UITabBarController,随你喜欢了。。。
ViewController.m

//
//  ViewController.m
//  Two
//
//  Created by maxh on 16/8/4.
//  Copyright © 2016年 merise. All rights reserved.
//

#import "ViewController.h"
#import "ChatDemoHelper.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [ChatDemoHelper shareHelper].mainVC = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这里是给主视图控制器赋值
最后不要忘了创建PCH文件把#import "EMSDKFull.h"

define DEMO_CALL 1 这两个放进去哈,,啦啦啦,完成了,可以接听视频了

更新:
环信3.2.0之后新增加了DemoCallManager这个类,然而并不用担心,你直接把环信demo里面的call那个文件里面的类导入工程,其他的不用动,就和原来的一样的

相关文章

  • 简单集成环信视频通话

    之前在弄环信的视频通话,找了好久都没有找到资源,尤其是简书上面,虽然环信官方有集成音视频的视频,但是那个是在太模糊...

  • 集成环信语音通话和视频通话

    由于项目中要用到环信的语音通话和视频通话服务,所以自己就根据环信官网上面的文档来进行集成。但是当看到环信的SDK集...

  • 环信

    环信EaseUI 导入环信SDK和EaseUI使用cocoapods集成 集成视频:http://www.imge...

  • iOS 环信集成

    1、流程类: 最新环信V3.3.7单聊集成与使用 iOS环信3.0集成 (三)单聊集成有视频 iOS 环信3.0D...

  • iOS 环信3.2.3 集成聊天,语音,视频,通话

    假定你已经集成了静态库,动态库,设置了bitcode,AppID绑定了推送,push Notifation 已经打...

  • IOS环信集成单聊

    最近几天在集成环信 ,文档大概也看了 简单说下集成步骤! 1.SDK集成和环信配置 iOS SDK 介绍及导入 ...

  •  集成环信IM(单聊)

    简单总结一下,仅对日后再次集成更加快速便捷,多数是对文档的简单概括. 一.快速集成环信SDK 1 注册环信账号,创...

  • ios环信3.0版本集成(包含音频通话)

    由于最近需求需要给app添加聊天功能,所以想到了环信,上一次集成环信是去年了,当时也只是简单的集成简单聊天功能,也...

  • iOS 环信集成视频

    环信,继续研究:EaseUI去集成单聊界面视频:http://community.easemob.com/vide...

  • 环信

    iOS 集成环信(四) 群聊iOS 环信集成(五) 加载会话列表

网友评论

  • 0d97265428a6:作者您好。我现在毕设在这方面陷入了困境。请您帮帮忙。万分感谢。
  • 403cd85e800d:我只想集成一个接收视频被呼叫的功能,能加个qq详细聊一下么,我的:30329533
    403cd85e800d:@风飞燕 其实我最没看懂的是怎么唤起接收视频聊天这个功能,都需要哪些文件?
    403cd85e800d:@风飞燕 恩,我用pod集成的环信,然后我还需要把什么文件导入工程?Call的那个文件夹,还有其他什么东西么?
    前端杜若:@如来撸管子 如果你只想做接收视频的功能,你就把己方呼出功能注释掉就行
  • 爱吃米饭的陈大鹰:请问一下环信的视频支持一种功能嘛?就是那种类似视频连麦一样,主播可以控制,在和A聊天的时候其他人不能聊天,然后A走之后下一个用户才能聊天?
    前端杜若:@爱吃米饭的陈大鹰 这个你要去问问环信技术,我没有弄过这个
  • c471f18d2f00:你那的视频通话画面清晰吗?我这码率调到最大5000了,画面还是很模糊
  • 南巷旧人M:大神您好,我现在的需求就是吧整个的环信的示例demo移植到我的app中,但是老报错,大神能不能给一个demo?
    南巷旧人M:@风飞燕 你有视频链接吗?
    南巷旧人M:@风飞燕 视频好像是老版本的
    前端杜若:@南巷旧人M 这个你可以看着环信的视频做,很清楚的
  • Mr_某某某:你好,我想请教一些关于 环信视频的问题.方便给个 扣扣号吗?
  • Hero_Guo:我只想实现双方简单的视屏通话, 该删哪些呢? 愁人...
    前端杜若:@Hero_Guo 你先能看到视频了在修改界面嘛,ChatDemoHelper这个类很重要
    前端杜若:@Hero_Guo 你需要CallViewController、ChatDemoHelper这两个类 ChatDemoHelper里面删掉不是#if DEMO_CALL == 1 ~#endif这里面的东西,CallViewController 你先删掉 不需要的头文件保留#import <CoreTelephony/CTCallCenter.h>
    #import <CoreTelephony/CTCall.h>
    #import "CallViewController.h"

    #import "ChatDemoHelper.h"
    保留这些,然后看着删吧
    前端杜若:我帮你看看,等会
  • Hero_Guo:需要在自己的工程中导入环信的 CellViewController 吗?
    前端杜若:@Hero_Guo 如果你要自己写界面也可以
    前端杜若:@Hero_Guo 需要,但是你要把不需要的代码删掉

本文标题:简单集成环信视频通话

本文链接:https://www.haomeiwen.com/subject/dlahsttx.html