title: 实战教程

本文档将给出一些详尽的示例教程。

示例说明

本教程以一对一视频通话为例,讲解如何通过 Video SDK 实现实时视频通话功能。

在此之前需要开启控制面板中的“实时视频通话”功能。

示例的最终的展示效果如下图:

video_quickstart_ios_conversation

具体步骤

1. 安装 SDK

要将 Wilddog SDK 导入到你的工程中,推荐使用 CocoaPods,如果没用过 CocoaPods,请先访问 CocoaPods getting started

打开工程目录,新建一个 Podfile 文件

  1. $ cd your-project-directory
  2. $ pod init
  3. $ open -a Xcode Podfile # opens your Podfile in XCode

然后在 Podfile 文件中添加以下语句

  1. pod 'WilddogVideo'

最后安装 SDK

  1. $ pod install
  2. $ open your-project.xcworkspace

2. 用户身份认证

视频通话的前提条件是要有可识别的用户身份,使用 Auth SDK 进行用户身份认证。在这里使用匿名登录实现身份认证。认证后会为每个用户分配唯一的 Wilddog ID。

  1. [WDGApp configureWithOptions:[[WDGOptions alloc] initWithSyncURL:@"https://<#appId#>.wilddogio.com"]];
  2. // 使用VideoSDK前必须经过WilddogAuth身份认证
  3. __weak __typeof__(self) weakSelf = self;
  4. [[WDGAuth auth] signInAnonymouslyWithCompletion:^(WDGUser *user, NSError *error) {
  5. __strong __typeof__(self) strongSelf = weakSelf;
  6. if (strongSelf == nil) {
  7. return;
  8. }
  9. if (error) {
  10. NSLog(@"请在控制台为您的AppID开启匿名登录功能,错误信息: %@", error);
  11. return;
  12. }
  13. // 匿名登录成功,进行后续操作
  14. }];

3. 初始化 Video SDK

用户身份认证成功后,可以初始化 Video SDK 。

  1. strongSelf.wilddogVideoClient = [[WDGVideoClient alloc] initWithApp:[WDGApp defaultApp]];
  2. strongSelf.wilddogVideoClient.delegate = self;

4. 实现用户列表

邀请对方加入视频通话,需要获取对方的在线状态。Video SDK 本身不提供获取在线用户列表功能,因此需要开发者使用 Sync SDK 来自己实现。用户登陆系统后将自己的 Wilddog ID 保存到用户列表中。

  1. // 将自己加入到用户列表中
  2. WDGSyncReference *userWilddog = [[self.syncReference child:@"users"] child:self.user.uid];
  3. [userWilddog setValue:@YES];
  4. [userWilddog onDisconnectRemoveValue];

数据库中的数据结构如图所示:

title: 实战教程 - 图2

5. 获取和预览本地视频

通过 Video SDK 获取本地视频流,并在视频展示控件中预览。

  1. - (void)createLocalStream
  2. {
  3. // 创建本地流
  4. WDGVideoLocalStreamOptions *localStreamOptions = [[WDGVideoLocalStreamOptions alloc] init];
  5. localStreamOptions.audioOn = YES;
  6. localStreamOptions.videoOption = WDGVideoConstraintsHigh;
  7. self.localStream = [[WDGVideoLocalStream alloc] initWithOptions:localStreamOptions];
  8. }
  9. - (void)previewLocalStream
  10. {
  11. // 将本地流展示到 `localVideoView` 中
  12. if (self.localStream != nil) {
  13. [self.localStream attach:self.localVideoView];
  14. }
  15. }

6. 发起视频通话

选择用户列表中的用户,发起视频通话。

  1. WDGVideoConnectOptions *connectOptions = [[WDGVideoConnectOptions alloc] initWithLocalStream:self.localStream];
  2. WDGVideoOutgoingInvite *outgoingInvitation = [self.wilddogVideoClient inviteToConversationWithID:userID options:connectOptions completion:^(WDGVideoConversation *conversation, NSError *error) {
  3. __strong __typeof__(self) strongSelf = weakSelf;
  4. if (strongSelf == nil) {
  5. return;
  6. }
  7. if (conversation != nil) {
  8. // 邀请成功
  9. strongSelf.videoConversation = conversation;
  10. strongSelf.videoConversation.delegate = strongSelf;
  11. } else {
  12. // 邀请失败
  13. NSString *errorMessage = [NSString stringWithFormat:@"邀请参与者错误(%@): %@", participantID, [error localizedDescription]];
  14. NSLog(@"%@",errorMessage);
  15. [strongSelf showAlertWithTitle:@"提示" message:errorMessage];
  16. }
  17. }];

7. 接受或拒绝邀请

发起视频通话后,被邀请人会收到邀请事件,被邀请人可以选择接受或拒绝该邀请,接受邀请则视频通话建立。

  1. // 展示弹窗让用户选择是否接受邀请
  2. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"%@ 邀请你进行视频通话", invite.fromUserID] preferredStyle:UIAlertControllerStyleAlert];
  3. UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  4. [invite reject];
  5. }];
  6. __weak __typeof__(self) weakSelf = self;
  7. UIAlertAction *acceptAction = [UIAlertAction actionWithTitle:@"接受" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  8. [invite acceptWithLocalStream:self.localStream completion:^(WDGVideoConversation *conversation, NSError *error) {
  9. __strong __typeof__(self) strongSelf = weakSelf;
  10. if (strongSelf == nil) {
  11. return;
  12. }
  13. if (error) {
  14. NSLog(@"error: %@", [error localizedDescription]);
  15. return ;
  16. }
  17. strongSelf.videoConversation = conversation;
  18. strongSelf.videoConversation.delegate = strongSelf;
  19. }];
  20. }];
  21. [alertController addAction:rejectAction];
  22. [alertController addAction:acceptAction];
  23. [self presentViewController:alertController animated:YES completion:nil];

8. 展示对方视频

视频通话建立成功后,在视频通话中能够获取到对方视频流,在视频展示控件中展示。

  1. - (void)conversation:(WDGVideoConversation *)conversation didConnectParticipant:(WDGVideoParticipant *)participant
  2. {
  3. // 将participant 的代理设置为自己,以便在获得音视频流时得到通知
  4. participant.delegate = self;
  5. }
  6. - (void)participant:(WDGVideoParticipant *)participant didAddStream:(WDGVideoRemoteStream *)stream
  7. {
  8. // 获得参与者音视频流,将其展示出来
  9. NSLog(@"receive stream %@ from participant %@", stream, participant);
  10. self.remoteStream = stream;
  11. [self.remoteStream attach:self.remoteVideoView];
  12. }

9. 离开视频通话

视频通话过程中,调用下面方法离开视频通话。

  1. // 断开视频通话
  2. [self.videoConversation disconnect];
  3. // 清理资源
  4. [self.remoteStream detach:self.remoteVideoView];
  5. self.remoteStream = nil;
  6. self.videoConversation = nil;

获取示例源码

点此获取完整的 示例源码