收藏iosiOS DeveloperiOS音视频开发

ios直播---基于LFLiveKit的直播项目

2017-08-28  本文已影响870人  筱贰笔

非常感谢大家利用自己宝贵的时间来阅读我的文章 ,  这篇文章主要写一个iOS系统下基于LFLiveKit的直播demo ,如果你正好需要 , 希望你看完后能够对你的提升有所帮助 , 当然,阅读中如果发现什么错误和可优化的地方 , 希望可以得到你的指点 , 有任何不妥的地方 欢迎指正。喜欢的可以关注下我的简书我的博客

       最近一直在研究直播相关的东西,在网上遨游了几天,也算是有点成果了。其实直播在移动端的工作并没有太多的东西,其中观众端的实现比较简单,这里主要说一下主播端的实现,主播端需要实现的也就是音视频采集、音视频处理、推流,刚开始最好先把这些自己走一遍,把原理和流程搞懂,推荐1小时学会:最简单的iOS直播推流(一)介绍,可以根据自己需要看一下。现在有很多第三方都可以帮你实现这些功能,包括金山的推流SDK,基本的功能都有而且免费,感兴趣的可以自己去看一下基于金山云直播推流SDK实现直播功能,下面就来说一下我们今天的主角LFLiveKit,它已经帮我们实现了视频采集、后台录制、美颜功能、支持h264、AAC编码,动态改变速率,RTMP传输等,我们开发的时候就很简单了,先上几张demo截图

废话不多说,开始你的第一个直播项目

新建项目不用我说了吧😆,把文件拖入你的项目或者在你的podfile加入 pod 'LFLiveKit' ,(它的美颜使用的是GPUImage,感兴趣的可以看一下GPUImage详细解析),在直播控制器引用头文件

 #import "LFLiveKit.h"

@property (nonatomic, strong)LFLiveSession *session;

下面看一下常用的一些属性

@property (nullable,nonatomic, weak) delegate;//代理方法 

@property (nonatomic, assign) BOOL running;//控制直播是开还是关的状态

@property (nonatomic, strong,null_resettable) UIView *preView;//视频图层

@property (nonatomic, assign) AVCaptureDevicePosition captureDevicePosition;//摄像头方向

@property (nonatomic, assign) BOOL beautyFace;//美颜开关

@property (nonatomic,assign) BOOL muted;//静音开关

@property (nullable,nonatomic, strong,readonly) LFLiveStreamInfo * streamInfo;//控制直播流的信息

@property (nonatomic,assign,readonly) LFLiveState state;//直播流上传的状态

@property (nonatomic,assign) BOOL showDebugInfo;//

@property (nonatomic,assign) NSUInteger reconnectInterval;//重连间隔

然后初始化配置

-(void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

//初始化LFLiveSession

[self RtmpInit];

}

配置的时候可以使用默认设置,也可以自定义采集和推流设置,previewView自己创建吧,相关代码就不贴了

LFLiveSession *session  = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration] captureType:LFLiveCaptureDefaultMask];

session.preView= previewView;

//设置代理

_session.delegate = self;

_session.running = YES;

开始直播,这里的RTMP_URL_1为推流地址

LFLiveStreamInfo *stream = [LFLiveStreamInfo new];

stream.url = RTMP_URL_1;

[self.session startLive:stream];

结束直播

[self.session stopLive];

到这里其实就完成一个直播的基本流程了,下面来看一下他的代理方法

1、直播状态,返回的是一个枚举类型

- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state ;

typedef NS_ENUM (NSUInteger, LFLiveState){

/// 准备

LFLiveReady = 0,

/// 连接中

LFLivePending = 1,

/// 已连接

LFLiveStart = 2,

/// 已断开

LFLiveStop = 3,

/// 连接出错

LFLiveError = 4,

///  正在刷新

LFLiveRefresh = 5

};

2、连接失败

- (void)liveSession:(LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode;

3、直播流的信息,如果需要显示当前流量和实时码率等信息可以在这个方法里实现

- (void)liveSession:(LFLiveSession *)session debugInfo:(LFLiveDebug *)debugInfo;

最后说一下闪光灯切换、静音开关等功能的使用

_session.torch =!_session.torch;//闪光灯开关

_session.muted = !_session.muted;//静音开关

//切换摄像头

AVCaptureDevicePosition devicePositon = self.session.captureDevicePosition;

self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;

最后再让我们来看一下美颜,我这里简单的做一个调节美颜程度和亮度的功能,创建两个UISlider,分别控制美颜和亮度,具体实现上代码

#pragma mark --美颜效果调节

- (void)sliderValueChage:(id)slider

{

UISlider *searchSlider = slider;

switch (searchSlider.tag) {

case 105:

{

self.session.beautyLevel = searchSlider.value/100;

NSString *voiceValue = [NSString stringWithFormat:@"%.0f",searchSlider.value];

_beautyValue.text = voiceValue;

CGFloat change = (_lastBeautyValue - searchSlider.value) *2;

if (searchSlider.value < 20) {

_beautyValue.textAlignment = NSTextAlignmentRight;

}else if (searchSlider.value>80)

{

_beautyValue.textAlignment = NSTextAlignmentLeft;

}else

{

_beautyValue.textAlignment = NSTextAlignmentCenter;

}

[UIView animateWithDuration:0.1 animations:^{

_beautyValue.x -= change;

}];

_lastBeautyValue = searchSlider.value;

}

break;

case 106:

{

self.session.brightLevel = searchSlider.value/100;

NSString *voiceValue = [NSString stringWithFormat:@"%.0f",searchSlider.value];

_brightValue.text = voiceValue;

CGFloat change = (_lastBrightValue - searchSlider.value) *2;

if (searchSlider.value < 20) {

_brightValue.textAlignment = NSTextAlignmentRight;

}else if (searchSlider.value>80)

{

_brightValue.textAlignment = NSTextAlignmentLeft;

}else

{

_brightValue.textAlignment = NSTextAlignmentCenter;

}

[UIView animateWithDuration:0.1 animations:^{

_brightValue.x -= change;

}];

_lastBrightValue = searchSlider.value;

}

break;

default:

break;

}

}

当然你也可以自定义你需要的美颜样式,这里就不多说了,还有截图、网络信号强度、电池电量等小功能,自己在demo里看吧,附上我自己写的demo地址ZQLiveDemo,写的不好凑合看一下😜。有需要做连麦功能的可以看一下这个ios直播连麦功能实现,如果帮到你的话不妨试试给个👍,发现不对的地方,欢迎指正

下面放一些不错的文章,可以看一下

iOS 直播 —— 推流

转 映客 LFLiveKit 推流

【如何快速的开发一个完整的iOS直播app】(原理篇)

最简单的基于FFmpeg的推流器(以推送RTMP为例)

基于GPUImage的实时美颜滤镜

声明:demo仅用于学习交流使用,不可实际应用于任何商业项目!

上一篇下一篇

猜你喜欢

热点阅读