博客> iOS - 声网版(天天狼人杀谁上麦谁音视频,多人视频)
iOS - 声网版(天天狼人杀谁上麦谁音视频,多人视频)
2017-12-12 19:32 评论:0 阅读:245 OS_JJ
ios - 网易云信(天天狼人杀谁上麦谁音视频 多人视频)

以下内容涵盖自己工程中的model,大概流程就是 创建AgoraRtcEngineKit , 设置setChannelProfile 和 setVideoProfile(声音参数和视频参数自己设置),然后加入meeting!

pragma mark 初始化视频相关

//音视频相关
- (void)createVideoAndAudio {
    self.agoreRtcKit = [AgoraRtcEngineKit sharedEngineWithAppId:@"82c9a7747d9b44809f5120eb4544ad0e" delegate:self];
//    AgoraYuvEnhancerObjc *yuvEnhance =[[AgoraYuvEnhancerObjc alloc]init];
//    [yuvEnhance turnOn];
    [self.agoreRtcKit :AgoraRtc_ChannelProfile_Communication];
    [self.agoreRtcKit setVideoProfile:AgoraRtc_VideoProfile_DEFAULT swapWidthAndHeight:NO];
    /// 多少毫秒反馈一下声音  平滑系数:3(官方建议)
    [self.agoreRtcKit enableAudioVolumeIndication:400 smooth:3];

    //此处用来设置POP回上一界面 不关闭一直开启的背景音乐,不设置,则上一界面的背景音乐没了!
    NSDictionary *dic = @{@"che.audio.keep.audiosession":@YES};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [_agoreRtcKit setParameters:str];

    [self joinMeetingWithName:_roomModel.number userID:0];

}
/// 加入房间
- (void)joinMeetingWithName:(NSString *)meetingID userID:(NSInteger)userNumber {
    NSLog(@"音视频SDK版本:%@", [AgoraRtcEngineKit getSdkVersion]);
    [_agoreRtcKit joinChannelByKey:nil channelName:meetingID info:@"开发者需加入的任何附加信息" uid:userNumber joinSuccess:^(NSString *channel, NSUInteger uid, NSInteger elapsed) {
        [UnityFunction bindVideoPlayer:_roomModel.ID memberId:[NSString stringWithFormat:@"%ld",(long)uid]];
        //扬声器相关
        [_agoreRtcKit setDefaultAudioRouteToSpeakerphone:YES];
        [_agoreRtcKit isSpeakerphoneEnabled];
        [_agoreRtcKit setEnableSpeakerphone:YES];

        [_agoreRtcKit enableLocalVideo:NO];
        [self.agoreRtcKit enableVideo];
        // 禁止传音/视频
        [self sendVideoStream:NO];
        // 控制屏幕常亮
//        [UIApplication sharedApplication].idleTimerDisabled = YES;
        // 重新刷新UI
        [mainVC reloadUI];
    }];
}

/// 离开会议(不管当前是否在通话中,都可以调用leaveChannel,没有副作用)
- (void)leaveMeeting {
    [self.agoreRtcKit leaveChannel:^(AgoraRtcStats *stat) {
//        [AppToast showFileWithString:@"离开会议成功"];
        // 不控制灭屏
//        [UIApplication sharedApplication].idleTimerDisabled = NO;
//        if (_videoView) {
            [_videoView removeFromSuperview];
            _videoView =nil;
            _agoreRtcKit =nil;
//        }
    }];
}
/// 接受视频
- (void)loadVideoWithUserID:(NSUInteger)userID isRemote:(BOOL)remote {
    if (self.videoView) {
        self.videoView.hidden =NO;
    }
    AgoraRtcVideoCanvas *distalCanvas =[[AgoraRtcVideoCanvas alloc]init];
    distalCanvas.uid = userID;
    distalCanvas.view =self.videoView;
    distalCanvas.renderMode =AgoraRtc_Render_Fit;
    if (remote) {
        // 显示远端图层
        [self.agoreRtcKit setupRemoteVideo:distalCanvas];
    } else {
        // 显示本地视频
        [self.agoreRtcKit setupLocalVideo:distalCanvas];
    }
}

/// 发送/暂停 音视频
- (void)sendVideoStream:(BOOL)isSend {
    [_agoreRtcKit muteLocalAudioStream:!isSend];
    [_agoreRtcKit muteLocalVideoStream:!isSend];
}
/// 接收/暂停音视频
- (void)receiveVideoStream:(BOOL)isReceive {
    [_agoreRtcKit muteAllRemoteAudioStreams:!isReceive];
    [_agoreRtcKit muteAllRemoteVideoStreams:!isReceive];
}

#pragma mark - 代理方法相关
#pragma mark =====>AgoraRtcEngineKitDelegate<======
- (void)rtcEngine:(AgoraRtcEngineKit *)engine didAudioMuted:(BOOL)muted byUid:(NSUInteger)uid {
    for (NSInteger i=0; i<_roomModel.players.count; i++) {
        PlayerModel *player = _roomModel.players[i];
        UIImageView *ima = _characView.audioArr[i];
        if (uid ==[player.memberId integerValue]) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                ima.hidden = muted;
            });
        }
    }
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine reportAudioVolumeIndicationOfSpeakers:(NSArray*)speakers totalVolume:(NSInteger)totalVolume {
    if (_roomModel.isStart) {
        return;
    } else {
        if (speakers.count !=0) {
            for (NSInteger i=0; i<_roomModel.players.count; i++) {
                PlayerModel *player = _roomModel.players[i];
                for (AgoraRtcAudioVolumeInfo *userInfo in speakers) {
                    if (player.user.ID.length >0 &&
                        [player.memberId integerValue]!=0 &&
                        userInfo.uid ==[player.memberId integerValue]) {
                        UIImageView *ima = _characView.audioArr[i];
                        ima.hidden = NO;
                    }
                }
            }
        } else {
            for (UIImageView *ima in _characView.audioArr) {
                ima.hidden = YES;
            }
        }
    }
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine firstLocalVideoFrameWithSize:
(CGSize)size elapsed:(NSInteger)elapsed {
    NSLog(@"本地第一帧视频显示");
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine firstRemoteVideoDecodedOfUid:
(NSUInteger)uid size:(CGSize)size elapsed:(NSInteger)elapsed {
    NSLog(@"远程第一帧视频解码");
    [self loadVideoWithUserID:uid isRemote:YES];
    [_agoreRtcKit startPreview];
}
- (void)rtcEngine:(AgoraRtcEngineKit *)engine firstRemoteVideoFrameOfUid:
(NSUInteger)uid size:(CGSize)size elapsed:(NSInteger)elapsed {
    NSLog(@"远程第一帧视频显示");

}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didJoinedOfUid:
(NSUInteger)uid elapsed:(NSInteger)elapsed {
    NSLog(@"有人加入=%ld", (unsigned long)uid);

}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didOfflineOfUid:
(NSUInteger)uid reason:(AgoraRtcUserOfflineReason)reason {
    NSLog(@"有人离开=%ld", (unsigned long)uid);

}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didVideoEnabled:(BOOL)enabled byUid:(NSUInteger)uid {
    NSLog(@"用户启用/关闭视频回调");
}

- (void)rtcEngineVideoDidStop:(AgoraRtcEngineKit *)engine {
    NSLog(@"视频停止回调");

}

- (void)rtcEngineConnectionDidInterrupted:(AgoraRtcEngineKit *)engine {
    NSLog(@"网络连接中断回调");
    [AppToast showFileWithString:@"网络中断,稍后再试"];

    /*
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"网络不好请等待" preferredStyle:UIAlertControllerStyleAlert];

    [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];

    [self presentViewController:alert animated:YES completion:nil];
     */
}

- (void)rtcEngineConnectionDidLost:(AgoraRtcEngineKit *)engin {
    NSLog(@"网络连接丢失回调(与服务器中断连接, 不处理-会自动连接)");
//    [AppToast showFileWithString:@"视频流连接丢失, 我们正努力找回"];
}
收藏
1
sina weixin mail 回到顶部