获取roomID、access_token

获取roomID、access_token

代码对接

1: 创建Publisher对象

先创建一个 Publisher 对象,我们后面主要用它来完成推流工作。

不过在创建 Publisher 对象之前,还需要您指定一个VHPublishConfig对象,该对象的用途是决定 Publisher 推流时各个环节的配置参数,比如推流用多大的分辨率、每秒钟要多少帧画面(FPS)等等。

VHPublishConfig 在configWithType之后便已经装配了一些我们反复调过的参数,如果您不需要自己定制这些配置,塞给Publisher对象就可以了。如果您有相关领域的经验基础,需要对这些默认配置进行调整,可以阅读进阶篇中的内容。

    // 创建 LivePushConfig 对象,该对象默认初始化为基础配置
    VHPublishConfig* config = [VHPublishConfig configWithType:VHPublishConfigTypeDefault];
    //在 config中您可以对推流的参数(如:前后置摄像头、分辨率、帧率,横竖屏推流等)做一些初始化操作,需要注意 config不能为nil
    _publisher = [[VHLivePublisher alloc] initWithConfig: config];

2: 设置并启动预览画面、delegate

  • delegate 推流事件代理
  • startCapture 启动手机摄像头采集画面,并将画面渲染到屏幕上。
    _publisher.delegate            = self;
    _publisher.preView.frame = _perView.bounds;
    [self.perView insertSubview:_publisher.preView atIndex:0];
    //开始视频采集、并显示预览界面
    [_publisher startCapture];

3: 开始推流

经过 step1 和 step2 的准备之后,用下面这段代码就可以启动推流了:

  • roomId 推流房间id 可以通过调用 api 创建
  • accessToken 可以通过调用 api 获得
    NSString* roomId = @"lss_xxxxxx";
    NSString* accessToken = @"xxxxxxxxxx";
    [_publisher startPublish:roomId accessToken:accessToken];

4: 切换前置或后置摄像头

默认是使用后置摄像头(可以通过修改 VHPublishConfig 的配置项 captureDevicePosition 来修改这个默认值),调用一次switchCamera 切换一次,注意切换摄像头前必须保证 VHPublishConfig 和 VHLivePublisher 对象都已经初始化。

// 默认是前置摄像头,可以通过修改 LivePushConfig 的配置项 frontCamera 来修改这个默认值
[_publisher switchCamera:AVCaptureDevicePositionBack];//AVCaptureDevicePositionFront

5: 打开或关闭闪光灯

只有后置摄像头才可以打开闪光灯,另外该接口需要在启动预览之后调用

// 默认是前置摄像头,可以通过修改 LivePushConfig 的配置项 frontCamera 来修改这个默认值
if(!frontCamera) {
    BOOL bEnable = YES;
    //bEnable为YES,打开闪光灯; bEnable为NO,关闭闪光灯
    BOOL result = [_publisher torchMode: bEnable];
    //result为YES,打开成功;result为NO,打开失败
}

6: 手动对焦

对焦点 [x,y] 取值范围[0.0 - 1.0]

// 手动对焦 point 对焦点 [x,y] [0.0 - 1.0]
 - (BOOL)focusCameraAtAdjustedPoint:(CGPoint)point;

7: 变焦

变焦取值范围[1.0 - xx] xx 是系统相机支持最大变焦值

//变焦
 - (BOOL)zoomCamera:(CGFloat)zoomSize;

8: 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];
}

9: 停止推流

结束推流很简单,不过要做好清理工作,因为用于推流的 VHLivePublisher是不能多实例并行运转的,所以清理工作不当会导致下次直播遭受不良的影响。

//停止推流
 - (void)stopRtmpPublish {
    [_publisher stopPublish];
    _publisher.delegate = nil;
}

10: 停止采集

停止音视频采集,并关闭预览画面

[_publisher stopCapture];//停止采集

11: 销毁publisher

销毁初始化数据,同步销毁,如果感觉销毁太慢,可以开线程去销毁

[_publisher destoryObject];

事件处理

  • 推流状态监听 SDK 通过 VHLivePublisherDelegate 代理来监听推流相关的事件
事件ID 数值 含义说明
VHPublishStatusNone 0
VHPublishStatusPushConnectSucceed 1 直播连接成功
VHPublishStatusUploadSpeed 2 直播上传速率
VHPublishStatusUploadNetworkException 3 发起端网络环境差
VHPublishStatusUploadNetworkOK 4 发起端网络环境恢复正常
  • 错误通知
错误类型 数值 含义说明
VHPublishErrorNone 0
VHPublishErrorPusherError 1 推流相关错误@{code:"10001" ,content: "xxxxx"}
VHPublishErrorAuthError 2 验证等相关错误
VHPublishErrorParamError 3 参数相关错误
VHPublishErrorCaptureError 4 采集相关错误