转载地址: https://github.com/SunLiner/MiaowShow
pod 'LFLiveKit'
// 推流处理
#import "ViewController.h"
#import <LFLiveKit/LFLiveKit.h>
@interface ViewController ()<LFLiveSessionDelegate>
@property (weak, nonatomic) IBOutlet UIButton *beautifulBtn;
@property (weak, nonatomic) IBOutlet UIButton *livingBtn;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
/** RTMP地址 */
@property (nonatomic, copy) NSString *rtmpUrl;
@property (nonatomic, strong) LFLiveSession *session;
@property (nonatomic, weak) UIView *livingPreView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setup];
}
- (void)setup{
self.beautifulBtn.layer.cornerRadius = 10;
self.beautifulBtn.layer.masksToBounds = YES;
self.livingBtn.backgroundColor = [UIColor colorWithRed:(216)/255.0 green:(41)/255.0 blue:(116)/255.0 alpha:1.0];
self.livingBtn.layer.cornerRadius = 10;
self.livingBtn.layer.masksToBounds = YES;
self.statusLabel.numberOfLines = 0;
self.session.captureDevicePosition = AVCaptureDevicePositionBack;
}
// 直播需要显示Uiview
- (UIView *)livingPreView
{
if (!_livingPreView) {
UIView *livingPreView = [[UIView alloc] initWithFrame:self.view.bounds];
livingPreView.backgroundColor = [UIColor clearColor];
livingPreView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:livingPreView atIndex:0];
_livingPreView = livingPreView;
}
return _livingPreView;
}
- (LFLiveSession*)session{
if(!_session){
/*** 默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏 ***/
LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
videoConfiguration.videoSize = CGSizeMake(360, 640);
videoConfiguration.videoBitRate = 800*1024;
videoConfiguration.videoMaxBitRate = 1000*1024;
videoConfiguration.videoMinBitRate = 500*1024;
videoConfiguration.videoFrameRate = 24;
videoConfiguration.videoMaxKeyframeInterval = 48;
videoConfiguration.landscape = NO;
videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:videoConfiguration captureType:LFLiveCaptureDefaultMask];
// 设置代理
_session.delegate = self;
_session.running = YES;
_session.preView = self.livingPreView;
}
return _session;
}
- (IBAction)closeAction:(id)sender {
if (self.session.state == LFLivePending || self.session.state == LFLiveStart){
[self.session stopLive];
}
}
- (IBAction)switchCamareAction:(id)sender {
AVCaptureDevicePosition devicePositon = self.session.captureDevicePosition;
self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
NSLog(@"切换前置/后置摄像头");
}
// 开启美颜
- (IBAction)beautifulAction:(UIButton *)sender {
sender.selected = !sender.selected;
// 默认是开启了美颜功能的
self.session.beautyFace = !self.session.beautyFace;
}
// 开始直播
- (IBAction)livingAction:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) { // 开始直播
LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
streamInfo.url = @"rtmp://192.168.8.164:1935/zbcs/room";
self.rtmpUrl = streamInfo.url;
[self.session startLive:streamInfo];
}else{ // 结束直播
[self.session stopLive];
self.statusLabel.text = [NSString stringWithFormat:@"状态: 直播被关闭\nRTMP: %@", self.rtmpUrl];
}
}
#pragma mark -- LFStreamingSessionDelegate
/** live status changed will callback */
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state{
NSString *tempStatus;
switch (state) {
case LFLiveReady:
tempStatus = @"准备中";
break;
case LFLivePending:
tempStatus = @"连接中";
break;
case LFLiveStart:
tempStatus = @"已连接";
break;
case LFLiveStop:
tempStatus = @"已断开";
break;
case LFLiveError:
tempStatus = @"连接出错";
break;
default:
break;
}
self.statusLabel.text = [NSString stringWithFormat:@"状态: %@\nRTMP: %@", tempStatus, self.rtmpUrl];
}
/** live debug info callback */
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug*)debugInfo{
}
/** callback socket errorcode */
- (void)liveSession:(nullable LFLiveSession*)session errorCode:(LFLiveSocketErrorCode)errorCode{
}
@end