本文将介绍如何在 iOS 端使用 KRtcEngine SDK 快速实现直播推流。详细接口文档及说明请点开查看 iOS端推流SDK
# platform: ios, '10.0'
use_frameworks!
target 'Your App' do
pod 'KRtcEngine', podspec => '请联系我们的技术支持获取 SDK 的 podspec 地址'
end
现在,我们已经将 KRtcEngine iOS SDK 集成到项目中了。接下来我们要在 ViewController 中调用 KRtcEngine iOS SDK 提供的核心 API 实现直播推流功能,API 调用时序见下图。
注意,在下文中: 您需要将 YourToken 替换成您自己生成的 Token; 您需要将 YourChannelId 替换为您生成 Token 时填入的频道名称; 您需要将 YourUid 替换成您自己生成的 uid。
在调用 KRtcEngine API 前,您需要在项目中导入 KRtcEngine 类,并定义一个 ksRtcEngine 变量。
// ViewController.h
#import <KRtcEngine/KRtcEngine.h>
@interface ViewController : UIViewController <KRtcEngineDelegate>
@property (nonatomic, strong) KRtcEngine *ksRtcEngine;
@end
根据场景需要,为您的项目创建视频通话的用户界面。我们推荐您在项目中添加如下元素:
// ViewController.m
#import <UIKit/UIKit.h>
@interface ViewController ()
@property (nonatomic, strong) UIView *localView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self buildUI];
[self initializeKRtcEngine];
[self setupLocalVideo];
[self setupMediaParameters];
[self startLivePush];
}
- (void)buildUI {
self.localView = [[UIView alloc] init];
self.localView.frame = CGRectMake(self.view.bounds.size.width - 90, 0, 90, 160);
[self.view addSubview:self.localView];
}
@end
调用 initEngineWithConfig:delegate: 创建并初始化 KRtcEngine 对象。 您还可以根据场景需要,在初始化时注册想要监听的回调事件。
// ViewController.m
- (void)initializeKRtcEngine {
// 初始化日志配置
NSArray* docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* logPath = [NSString stringWithFormat:@"%@/krtc_sdk_logs", docPaths[0]];
KLogConfig* logConfig = [[KLogConfig alloc] init];
logConfig.level = KRtcEngineLogLevelDebug;
logConfig.filePath = [NSString stringWithFormat:@"%@/krtc.log", logPath];
logConfig.fileSize = 10 * 1024;
// 初始化 KRtcEngine 配置
KRtcEngineConfig *config = [[KRtcEngineConfig alloc] init];
config.appId = @"YourAppId";
config.logConfig = logConfig;
// 创建 KRtcEngine 实例
self.ksRtcEngine = [[KRtcEngine alloc] initEngineWithConfig:config delegate:self];
}
在开始推流前设置本地视图,以便在通话中看到本地图像。参考以下步骤设置本地视图:
// ViewController.m
- (void)setupLocalVideo {
[self.ksRtcEngine enableVideo];
KRtcEngineVideoCanvas* canvas = [[KRtcEngineVideoCanvas alloc] initWithUIView:self.localView channelId:@"YourChannelId" uid:self.@"YourUserId" renderMode:KRtcEngineVideoRenderModeScaleToFit mirrorMode:KRtcEngineVideoMirrorModeAuto];
[self.ksRtcEngine bindLocalVideoView:canvas];
[self.ksRtcEngine startPreview];
}
调用 setVideoEncoderConfiguration:videoEncoderConfig: 设置视频编码的分辨率,码率,帧率等参数;调用 setLiveAudioProfile:profile: 设置音频相应 profile。
// ViewController.m
- (void)setupMediaParameters {
// 设置视频编码参数
KRtcEngineVideoEncoderConfiguration* config = [[KRtcEngineVideoEncoderConfiguration alloc] initWithWidth:720 height:1280 frameRate:15 bitrate:1500];
[self.ksRtcEngine setVideoEncoderConfiguration:@"YourChannelId" videoEncoderConfig:config];
// 设置音频编码参数
[self.ksRtcEngine setLiveAudioProfile:@"YourChannelId" profile:KRtcEngineLiveAudioProfileMusicHigherQualityStereo];
}
// ViewController.m
- (void)KRtcEngine:(KRtcEngine*)engine
channelId:(NSString*)channelId
didPushStreamStateChanged:(KRtcEnginePushStreamState)state
error:(KRtcEnginePushStreamError)error {
switch (state) {
case KRtcEnginePushStreamStateRunning:
// do sth.
break;
case KRtcEnginePushStreamStateFailed:
// do sth.
break;
default:
break;
}
}
调用 startPushByToken:channelId:streamUrl: 开始直播。
// ViewController.m
- (void)startLivePush {
[self.ksRtcEngine startPushByToken:@"YourToken" channelId:@"YourChannelId" streamUrl:@"rtmp://yourstreamurl"];
}
根据场景需要,如停止直播、关闭 app 或 app 切换至后台时,调用 stopPush 停止直播。
// ViewController.m
[self.ksRtcEngine stopPush:@"YourChannelId"];
停止推流后,如果您想释放 KRtcEngine SDK 使用的所有资源,需调用 destroy 销毁 KRtcEngine 对象。
// ViewController.m
[self.ksRtcEngine destroy];
在运行项目前,您需要设置签名和开发团队,并添加设备权限。
在 Xcode 中,打开 info.plist 文件。在右侧列表中添加如下内容,获取相应的设备权限:
Key | Type | Value |
---|---|---|
Privacy - Microphone Usage Description | String | 使用麦克风的目的,例如:for a call or live interactive streaming。 |
Privacy - Camera Usage Description | String | 使用摄像头的目的,例如:for a call or live interactive streaming。 |
我们建议在 iOS 真机中运行您的项目。运行成功后,您可以看到本地视频,同时可以通过 ffplay 等工具拉取到直播流(拉流地址由业务层根据指定规则生成)。