最近更新时间:2023.06.05 21:38:35
首次发布时间:2023.06.05 21:38:35
本文介绍如何实现 iOS 开播 SDK 的基础功能。
在 Xcode 的 App Target 中,打开定义如何进入直播间的文件并添加以下代码:
说明
通过调用 CreateActivityAPIV2 或 ListActivityAPI 获取直播间的活动 ID,调用 GetTemporaryLoginTokenAPI 获取开播密钥。
#import <BDLive/BDLive.h> // 1. 创建 BDLLiveStreamingModel 实例 BDLLiveStreamingModel *model = [[BDLLiveStreamingModel alloc] init]; // 1.1 输入获取到的直播间活动 ID 和开播密钥 model.activityId = xxx; model.secretKey = xxx; // 1.2 (可选)配置美颜功能。通过离线或在线方式获取授权 model.effectConfig = [[BDLEffectConfig alloc] init]; // 离线授权 model.effectConfig.licenseMode = BDLEffectConfigLicenseModeOffline; model.effectConfig.licenseName = @"xxx.licbag"; // CV License 名称 // 在线授权 model.effectConfig.licenseMode = BDLEffectConfigLicenseModeOnline; model.effectConfig.key = @"xxx"; // 业务标识,对应开通的业务类型,请联系技术支持获取 model.effectConfig.secret = @"xxx"; // 业务密钥,创建业务时同key 一起获得,请联系技术支持获取 model.effectConfig.licenseName = @"xxx"; // CV License 名称 // 1.3(可选)配置录屏直播功能。该功能仅适用于 iOS 12 及以上版本。有关如何实现该功能的详细信息,请参考高级功能 > 录屏直播 if (@available(iOS 12.0, *)) { model.extensionBundleId = @"xxxxx"; // 屏幕共享扩展的 Bundle Identifier model.groupId = @"group.xxxxxx"; // App group 的 Container ID } // 2. 进入直播间 [[BDLLiveStreaming sharedInstance] joinLiveStreamingWithModel:model success:^(void) { // 3. 如果成功进入直播间,获取直播间页面 BDLLiveStreamingController *livePushVC = [[BDLLiveStreaming sharedInstance] getLiveStreamingController]; livePushVC.modalPresentationStyle = UIModalPresentationFullScreen; // 3.1 设置直播间代理,监听直播间变化。详见设置直播间事件回调 livePushVC.delegate = self; // 3.2(可选)配置直播间。详见自定义配置 // 4. 显示直播间 ViewController(livePushVC) [self presentViewController:livePushVC animated:YES completion:^{ [BDLDFloatLogView bringFloatToFrontIfNeeded]; }]; } failure:^(NSError * _Nonnull error) { // Handle error }]; // MARK: - BDLLivePushControllerDelegate // 在代理中关闭直播间页面 - (void)liveStreamingControllerCloseButtonDidClick:(BDLLiveStreamingController *)controller { [controller dismissViewControllerAnimated:YES completion:^{ }]; }
在直播间提供一个撑满屏幕且层级在当前界面元素之上的UI区域,供您添加自定义组件。
/// 内容视图,可添加自定义组件。大小和 liveStreamingVC.view 相同 @property (nonatomic, strong) UIView *contentView; /// 可以通过以下方式添加自定义组件 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(14, 100, 100, 100)]; [liveStreamingVC.contentView addSubview:view];
设置开播后是否显示评论区的消息和输入框。
/// 设置是否显示评论视图,默认显示 @property (nonatomic, copy, nullable) __kindof UIView *_Nullable (^customizedCommentView)(UIView *commentView); /// 不显示评论视图 self.customizedCommentView = ^__kindof UIView * _Nullable(UIView * _Nonnull commentView) { return nil; };
在代理中,设置回调监听直播间变化。
/// 在直播间需要被关闭时触发该回调。您应该在该回调中关闭直播间 - (void)liveStreamingControllerCloseButtonDidClick:(BDLLiveStreamingController *)controller; /// 在屏幕朝向改变时触发该回调 - (void)liveStreamingController:(BDLLiveStreamingController *)controller orientationDidChange:(UIInterfaceOrientation)orientation; /// 在直播开始时触发该回调。此时会隐藏开始直播和顶部的功能按钮,并改变美颜图标的位置 - (void)liveStreamingControllerDidStartStreaming:(BDLLiveStreamingController *)controller; /// 在直播结束时触发该回调 - (void)liveStreamingControllerDidEndStreaming:(BDLLiveStreamingController *)controller; /// 生命周期回调,分别对应 viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear 和 viewDidDisappear 方法 - (void)liveStreamingControllerDidLoad:(BDLLiveStreamingController *)controller; - (void)liveStreamingControllerWillAppear:(BDLLiveStreamingController *)controller animated:(BOOL)animated; - (void)liveStreamingControllerDidAppear:(BDLLiveStreamingController *)controller animated:(BOOL)animated; - (void)liveStreamingControllerWillDisappear:(BDLLiveStreamingController *)controller animated:(BOOL)animated; - (void)liveStreamingControllerDidDisappear:(BDLLiveStreamingController *)controller animated:(BOOL)animated;