iOS SDK 如何发起直播
推流准备
-
获取roomId
-
获取直播间包含推流权限的access_token
工程配置
在开始使用sdk之前,我们要配置好IDE和创建基础的工程代码。相关内容在【工程配置】中有详细说明。
注意:发起直播只能在真机环境下运行
时序图

注:已知直播间ID的情况下虚线框部分无需请求。
代码对接
1: 引入推流头文件
#import "VHLivePublisher.h"
2: 创建Publisher对象
先创建一个 Publisher 对象,我们后面主要用它来完成推流工作。
不过在创建 Publisher 对象之前,还需要您指定一个 VHPublishConfig 对象,该对象的用途是决定 Publisher 推流时各个环节的配置参数,比如推流用多大的分辨率、每秒钟要多少帧画面(FPS)等等,具体配置参数移步【VHPublishConfig 参数说明】。
VHPublishConfig 在 configWithType 之后便已经装配了一些我们反复调过的参数,如果您不需要自己定制这些配置,塞给Publisher对象就可以了。如果您有相关领域的经验基础,需要对这些默认配置进行调整。
// 创建 LivePushConfig 对象,该对象默认初始化为基础配置
VHPublishConfig* config = [VHPublishConfig configWithType:VHPublishConfigTypeDefault];
//在 config中您可以对推流的参数(如:前后置摄像头、分辨率、帧率,横竖屏推流等)做一些初始化操作,需要注意 config不能为nil
_publisher = [[VHLivePublisher alloc] initWithConfig: config];
3: 设置并启动预览画面、delegate
- delegate 推流事件代理
- startCapture 启动手机摄像头采集画面,并将画面渲染到屏幕上。
_publisher.delegate = self;
_publisher.preView.frame = self.view.bounds;
[self.view insertSubview:_publisher.preView atIndex:0];
//开始视频采集、并显示预览界面
[_publisher startCapture];
4: 开始推流
经过 step1 和 step2 的准备之后,用下面这段代码就可以启动推流了:
roomId 和 accessToken 参考页首推流准备
NSString* roomId = @"lss_xxxxxx";
NSString* accessToken = @"xxxxxxxxxx";
[_publisher startPublish:roomId accessToken:accessToken];
5: 切换前置或后置摄像头
默认是使用后置摄像头(可以通过修改 VHPublishConfig 的配置项 captureDevicePosition 来修改这个默认值),调用一次switchCamera 切换一次,注意切换摄像头前必须保证 VHPublishConfig 和 VHLivePublisher 对象都已经初始化。
// 默认是前置摄像头,可以通过修改 LivePushConfig 的配置项 frontCamera 来修改这个默认值
[_publisher switchCamera:AVCaptureDevicePositionBack];//AVCaptureDevicePositionFront
6: 打开或关闭闪光灯
只有后置摄像头才可以打开闪光灯,另外该接口需要在启动预览之后调用
// 默认是前置摄像头,可以通过修改 LivePushConfig 的配置项 frontCamera 来修改这个默认值
if(!frontCamera) {
BOOL bEnable = YES;
//bEnable为YES,打开闪光灯; bEnable为NO,关闭闪光灯
BOOL result = [_publisher torchMode: bEnable];
//result为YES,打开成功;result为NO,打开失败
}
7: 手动对焦
对焦点 [x,y] 取值范围[0.0 - 1.0]
// 手动对焦 point 对焦点 [x,y] [0.0 - 1.0]
- (BOOL)focusCameraAtAdjustedPoint:(CGPoint)point;
8: 变焦
变焦取值范围[1.0 - xx] xx 是系统相机支持最大变焦值
//变焦
- (BOOL)zoomCamera:(CGFloat)zoomSize;
9: APP 切入后台
常规模式下,App一旦切到后台,摄像头的采集能力就会被 iOS 暂时停止掉,这就意味着 SDK 不能再继续采集并编码出音视频数据。
a. 注册切入后台通知
//先注册后退消息通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
b. 切后台处理
//收到通知后,调用beginBackgroundTaskWithExpirationHandler
- (void)handleEnterBackground:(NSNotification *)notification
{
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
}];
[_publisher pause];
}
c. 切前台处理
//切前台处理
- (void)handleEnterForeground:(NSNotification *)notification
{
[_publisher resume];
}
10: 停止推流
结束推流很简单,不过要做好清理工作,因为用于推流的 VHLivePublisher是不能多实例并行运转的,所以清理工作不当会导致下次直播遭受不良的影响。
//停止推流
- (void)stopRtmpPublish {
[_publisher stopPublish];
_publisher.delegate = nil;
}
11: 停止采集
停止音视频采集,并关闭预览画面
[_publisher stopCapture];//停止采集
12: 销毁publisher
销毁初始化数据,同步销毁,如果感觉销毁太慢,可以开线程去销毁
[_publisher destoryObject];
事件处理
- 推流状态监听 SDK 通过 VHLivePublisherDelegate 代理来监听推流相关的事件
采集到第一帧画面回调
/**
* 采集到第一帧的回调
* @param image 第一帧的图片
*/
-(void)firstCaptureImage:(UIImage*)image;
推流状态回调
/**
* 推流状态回调
* @param status 状态类型
* @param info 状态信息
*/
- (void)onPublishStatus:(VHPublishStatus)status info:(NSDictionary*)info;
事件ID | 数值 | 含义说明 |
---|---|---|
VHPublishStatusPushConnectSucceed | 1 | 直播连接成功 |
VHPublishStatusUploadSpeed | 2 | 直播上传速率 |
VHPublishStatusUploadNetworkException | 3 | 发起端网络环境差 |
VHPublishStatusUploadNetworkOK | 4 | 发起端网络环境恢复正常 |
错误回调
/**
* 错误回调
* @param error 错误类型
* @param info 错误信息
*/
- (void)onPublishError:(VHPublishError)error info:(NSDictionary*)info;//@{code:"" ,content: ""}
错误类型 | 数值 | 含义说明 |
---|---|---|
VHPublishErrorPusherError | 1 | 推流相关错误@{code:"10001" ,content: "xxxxx"} |
VHPublishErrorAuthError | 2 | 验证等相关错误 |
VHPublishErrorParamError | 3 | 参数相关错误 |
VHPublishErrorCaptureError | 4 | 采集相关错误 |
快速集成代码
可以把推流端代码写到 ViewController.m 的 viewDidLoad 方法中测试推流,但是要在真机环境下运行,代码如下:
//
// ViewController.m
// TestProject
//
// Created by vhall on 2018/3/12.
// Copyright © 2018年 vhall. All rights reserved.
//
#import "ViewController.h"
#import "VHLivePublisher.h"
@interface ViewController ()<VHLivePublisherDelegate>
@property (nonatomic,strong)VHLivePublisher *publisher;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建 LivePushConfig 对象,该对象默认初始化为基础配置
VHPublishConfig* config = [VHPublishConfig configWithType:VHPublishConfigTypeDefault];
//在 config中您可以对推流的参数(如:前后置摄像头、分辨率、帧率,横竖屏推流等)做一些初始化操作,需要注意 config不能为nil
_publisher = [[VHLivePublisher alloc] initWithConfig: config];
//设置代理
_publisher.delegate = self;
//设置预览界面位置
_publisher.preView.frame = self.view.bounds;
//把预览界面加入页面中
[self.view insertSubview:_publisher.preView atIndex:0];
//开始视频采集、并显示预览界面
[_publisher startCapture];
//开始推流
NSString* roomId = @"lss_xxxxxx";//roomId 获得方式 参考页面开始处
NSString* accessToken = @"xxxxxxxxxx";//accessToken 获得方式 参考页面开始处
[_publisher startPublish:roomId accessToken:accessToken];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLayoutSubviews
{
//更新览界面位置
_publisher.preView.frame = self.view.bounds;
}
#pragma mark - VHLivePublisherDelegate
/**
* 采集到第一帧的回调
* @param image 第一帧的图片
*/
-(void)firstCaptureImage:(UIImage*)image
{
}
/**
* 推流状态回调
* @param status 状态类型
* @param info 状态信息
*/
- (void)onPublishStatus:(VHPublishStatus)status info:(NSDictionary*)info
{
NSLog(@"%ld %@",(long)status,info[@"content"] );
}
/**
* 错误回调
* @param error 错误类型
* @param info 错误信息
*/
- (void)onPublishError:(VHPublishError)error info:(NSDictionary*)info//@{code:"" ,content: ""}
{
NSLog(@"错误:%ld %@",(long)error,info[@"content"] );
}
@end