ios基于LFLiveKit的直播项目

NumbersHenl 5年前
   <p>最近一直在研究直播相关的东西,在网上遨游了几天,也算是有点成果了。其实直播在移动端的工作并没有太多的东西,也就音视频采集、音视频处理、推流,刚开始最好先把这些自己走一遍,把原理和流程搞懂,最后我会放一些我找的一些资料,可以根据自己需要看一下。现在有很多第三方都可以帮你实现这些功能,包括金山的推流SDK,基本的功能都有而且免费,感兴趣的可以自己去看一下金山云直播推流SDK,后面我会写一个关于金山云直播的简书,下面就来说一下我们今天的主角LFLiveKit,它已经帮我们实现了视频采集、后台录制、美颜功能、支持h264、AAC编码,动态改变速率,RTMP传输等,我们开发的时候就很简单了,先上几张demo截图</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a398f458769ca6a9ea590d2c1740afac.jpg"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d6f7c2aa91779038aaf02599d28a9084.jpg"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/00c3100caca28c86c366426b6d63f6fc.jpg"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/395e76ffe0021899e60b6136598f863f.jpg"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/5930cb191623de37963d4e3e05385446.jpg"></p>    <p>废话不多说,开始你的第一个直播项目</p>    <p>新建项目不用我说了吧????,把文件拖入你的项目或者在你的podfile加入 pod 'LFLiveKit' ,(它的美颜使用的是GPUImage,感兴趣的可以看一下 <a href="/misc/goto?guid=4959751752888776050" rel="nofollow,noindex">GPUImage详细解析</a> ),在直播控制器引用头文件</p>    <pre>  <code class="language-objectivec"> #import "LFLiveKit.h"</code></pre>    <pre>  <code class="language-objectivec">@property (nonatomic, strong)LFLiveSession *session;</code></pre>    <p>下面看一下常用的一些属性</p>    <pre>  <code class="language-objectivec">@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;//重连间隔</code></pre>    <p>然后初始化配置,因为我这里有更改设置的需求,所以就在viewDidAppear里做的初始化配置</p>    <pre>  <code class="language-objectivec">-(void)viewDidAppear:(BOOL)animated  {  [super viewDidAppear:animated];  //初始化LFLiveSession  [self RtmpInit];  }</code></pre>    <p>配置的时候可以使用默认设置,也可以自定义采集和推流设置,previewView自己创建吧,相关代码就不贴了</p>    <pre>  <code class="language-objectivec">LFLiveSession *session  = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration] captureType:LFLiveCaptureDefaultMask];  session.preView= previewView;  //设置代理  _session.delegate = self;  _session.running = YES;</code></pre>    <p>开始直播,这里的RTMP_URL_1为推流地址</p>    <pre>  <code class="language-objectivec">LFLiveStreamInfo *stream = [LFLiveStreamInfo new];  stream.url = RTMP_URL_1;  [self.session startLive:stream];</code></pre>    <p>结束直播</p>    <pre>  <code class="language-objectivec">[self.session stopLive];</code></pre>    <p>下面来看一下他的代理方法</p>    <p>1、直播状态,返回的是一个枚举类型</p>    <pre>  <code class="language-objectivec">- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state ;</code></pre>    <pre>  <code class="language-objectivec">typedef NS_ENUM (NSUInteger, LFLiveState){  /// 准备  LFLiveReady = 0,  /// 连接中  LFLivePending = 1,  /// 已连接  LFLiveStart = 2,  /// 已断开  LFLiveStop = 3,  /// 连接出错  LFLiveError = 4,  ///  正在刷新  LFLiveRefresh = 5  };</code></pre>    <p>2、连接失败</p>    <pre>  <code class="language-objectivec">- (void)liveSession:(LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode;</code></pre>    <p>3、直播流的信息,如果需要显示当前流量和实时码率等信息可以在这个方法里实现</p>    <pre>  <code class="language-objectivec">- (void)liveSession:(LFLiveSession *)session debugInfo:(LFLiveDebug *)debugInfo;</code></pre>    <p>最后说一下闪光灯切换、静音开关等功能的使用</p>    <pre>  <code class="language-objectivec">_session.torch =!_session.torch;//闪光灯开关</code></pre>    <pre>  <code class="language-objectivec">_session.muted = !_session.muted;//静音开关</code></pre>    <pre>  <code class="language-objectivec">//切换摄像头  AVCaptureDevicePosition devicePositon = self.session.captureDevicePosition;  self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;</code></pre>    <p>最后再让我们来看一下美颜,我这里简单的做一个调节美颜程度和亮度的功能,创建两个UISlider,分别控制美颜和亮度,具体实现上代码</p>    <pre>  <code class="language-objectivec">#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;  }  }</code></pre>    <p>当然你也可以自定义你需要的美颜样式,这里就不多说了,附上我自己写的demo地址 <a href="/misc/goto?guid=4959751752983810324" rel="nofollow,noindex">ZQLiveDemo</a> ,写的不好凑合看一下????。如发现不对的地方,欢迎指正</p>    <p>下面放一些不错的文章,可以看一下</p>    <p><a href="/misc/goto?guid=4959751753078768341" rel="nofollow,noindex">iOS 直播 —— 推流</a></p>    <p><a href="/misc/goto?guid=4959751753156832764" rel="nofollow,noindex">转 映客 LFLiveKit 推流</a></p>    <p><a href="/misc/goto?guid=4959728914646286138" rel="nofollow,noindex">【如何快速的开发一个完整的iOS直播app】(原理篇)</a></p>    <p><a href="/misc/goto?guid=4959751753271362440" rel="nofollow,noindex">最简单的基于FFmpeg的推流器(以推送RTMP为例)</a></p>    <p><a href="/misc/goto?guid=4959751753354028060" rel="nofollow,noindex">基于GPUImage的实时美颜滤镜</a></p>    <p> </p>    <p>来自:http://www.cocoachina.com/ios/20170828/20393.html</p>    <p> </p>