之前在弄環(huán)信的視頻通話,找了好久都沒有找到資源妻熊,尤其是簡書上面,雖然環(huán)信官方有集成音視頻的視頻仑最,但是那個是在太模糊了扔役,我這里只能被動接收視頻,下面開始吧
我用的SDK是這個ios_IM_sdk_V3.1.4
1.將HyphenateFullSDK導(dǎo)入項目中然后
這些環(huán)信官方文檔都有滴
2.登錄注冊的接口加入項目中
重點(diǎn)來了
3
ChatDemoHelper.h 我進(jìn)行了刪減
/************************************************************
* * 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 = @"對方?jīng)]有回應(yīng)";
}
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:@"沒有響應(yīng)警医,自動掛斷" 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:@"創(chuàng)建實時通話失敗亿胸,請稍后重試" 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:@"網(wǎng)絡(luò)連接失敗" 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
這里需要注意一點(diǎn),ViewController.h 是根據(jù)你的主視圖控制器來進(jìn)行更改预皇,環(huán)信上面是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
這里是給主視圖控制器賦值
最后不要忘了創(chuàng)建PCH文件把#import "EMSDKFull.h"
define DEMO_CALL 1 這兩個放進(jìn)去哈,,啦啦啦酿炸,完成了宾添,可以接聽視頻了
更新:
環(huán)信3.2.0之后新增加了DemoCallManager這個類,然而并不用擔(dān)心掷漱,你直接把環(huán)信demo里面的call那個文件里面的類導(dǎo)入工程粘室,其他的不用動榄檬,就和原來的一樣的