之前在弄环信的视频通话,找了好久都没有找到资源,尤其是简书上面,虽然环信官方有集成音视频的视频,但是那个是在太模糊了,我这里只能被动接收视频,下面开始吧
我用的SDK是这个ios_IM_sdk_V3.1.4
1.将HyphenateFullSDK导入项目中然后
这些环信官方文档都有滴
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那个文件里面的类导入工程,其他的不用动,就和原来的一样的