初始化

概述

集成 SDK 完成后,要想使用 SDK 的功能,需要先对SDK 进行 初始化鉴权 ,然后才能正常使用。

初始化步骤

申请AppID和AppSecret

请在 LinkV 开发者后台 申请 SDK 鉴权需要的 appid签名获取 AppID 和 AppSecret 指引

初始化SDK

[StrangerChat createEngine:your_app_id appKey:your_app_sign isTestEnv:NO completion:^(NSInteger code) {
  if (code == 0) {
        NSLog(@"SDK init succeed");
    }
 } delegate:self];

实现IM连接状态回调

- (void)onQueryIMToken {
    // 向你的服务端请求IMToken,你的服务端再向IM服务端请求IMToken,之后再返回给客户端,客户端再设置给SDK
    [[LVIMSDK sharedInstance] setIMToken:self.uid token:@"XXX"];
}

- (void)onIMTokenExpired:(NSString *)uid token:(NSString *)token owner:(NSString *)owner {
    // IMToken失效
}

- (void)onIMAuthSuccessed:(NSString *)uid token:(NSString *)token unReadMsgSize:(int)unReadMsgSize {
    // IM正常使用
    NSLog(@"[Wing] onIMAuthSuccessed");
}

呼叫、接听、挂断

  1. 传入某个用户的 user_id 进行呼叫
[self.engine call:"user_id" isAudio:NO extra:nil callback:nil];
  1. 被呼叫的用户会收到呼叫回调,在回调中进行接听或者挂断操作
- (int)onCallReceived:(NSString *)uid isAudio:(BOOL)isAudio timestamp:(int64_t)timestamp extra:(NSString *)extra {
    
    // 接听电话
    [self.engine anwserCall:uid accept:NO isAudio:isAudio extra:@"time ount" callback:nil];
    return 0;
}
  1. 接听或挂断某个用户的来电
// accept:YES接听,NO挂断
[self.engine anwserCall:uid accept:NO isAudio:isAudio extra:@"time ount" callback:nil];
  1. 收到某个用户接听或挂断的回调
- (int)onAnwserCallReceived:(NSString *)uid accept:(BOOL)accept isAudio:(BOOL)isAudio  timestamp:(int64_t)timestamp extra:(NSString *)extra {

    // 收到同意接听电话的回调
  return 0;
}

- (int)onHangupCallReceived:(NSString *)uid extra:(NSString *)extra {
    NSLog(@"[Wing] onHangupCallReceived uid = %@ extra = %@", uid, extra);
    
    // 收到挂断通话的回调
    return 0;
}

房间

  1. 进入房间
[self.engine loginRoom:"user_id" roomId:"room_id" isHost:self.isCaller delegate:self];
  1. 当收到 onRoomConnected 的时候表示进入房间成功
    • 调用 SDKstartPreview 开始预览,startPreview 返回的是视频流 View ,移动此 View 就能达到移动视频流的效果
    • 调用 SDKstartPublishing 开始推流
// 进入房间成功
- (void)onRoomConnected:(NSString *)roomId {
    NSLog(@"[Wing] onLoginRoomSucceed thread %@", [NSThread currentThread]);
    
    // 开始预览
    self.meView = [self.engine startPreview:self.smallView];
    // 开始推流
    [self.engine startPublishing];    
}
  1. 在收到 onAddRemoter 的时候调用 SDKstartPlayingStream 播放对方视频流, startPlayingStream 返回的是对方视频流 View,移动此View 就能达到移动视频流的效果
// 收到对方视频流回调
- (void)onAddRemoterUser:(NSString *)uid {
    
    self.otherView = [self.engine startPlayingStream:uid inView:self.bigView];
    [self addNoCameraDefaultView:NO];
}
  1. 在收到 onRemoteLeave 的回调时调用 SDKstopPlayingStream 停止播放对方视频流
- (void)onRemoteLeave:(NSString *)uid {
    [self.engine stopPlayingStream:uid];    
}
  1. 房间中的其它回调
    • 对方关闭或者打开麦克风
    • 对方关闭或者打开摄像头
    • 收到礼物
    • 房间断开连接
// 对方关闭或者打开麦克风的回调
- (int)onUserMicrophoneChanged:(NSString *)uid roomId:(NSString *)roomId open:(BOOL)isOpen {
    NSLog(@"[Wing] onUserMicrophoneChanged uid = %@ roomId = %@ open = %d", uid, roomId, isOpen);
    return 0;
}
// 对方关闭或打开摄像头的回调
- (int)onUserVideoCameraChanged:(NSString *)uid roomId:(NSString *)roomId open:(BOOL)isOpen {
    NSLog(@"[Wing] onUserVideoCameraChanged uid = %@ roomId = %@ open = %d", uid, roomId, isOpen);
    if (![uid isEqualToString:self.user.uid]) return 0;
    
    self.otherDefaultView.hidden = isOpen;
    return 0;
}
// 收到礼物消息
- (int)onGiftReceived:(NSString *)giftId count:(NSInteger)count sendUid:(NSString *)sendUid uid:(NSArray<NSString *> *)uids roomId:(NSString *)roomId extra:(NSString *)extra {
    NSLog(@"[Wing] onGiftReceived giftId = %@", giftId);
    
    int gid = [giftId intValue];
    LVGiftModel *gift = [LVHelper shared].gifts[gid];
    [self showGift: gift];
    
    [[LVHelper shared] changeBalance:-gift.giftPrice];
    
    return 0;
}

/**
 * 被踢出房间或断开房间链接
 * @param errorCode 错误码,参见Zego或LinkV错误码
 * @param roomId 房间id
 */
- (void)onRoomDisconnect:(int)errorCode roomId:(NSString *)roomId {
    NSLog(@"[Wing] onRoomDisconnect roomId = %@ errorCode = %@", roomId, @(errorCode));
}