iOS 推流LFLiveKit

移动开发 简书

关于直播推流所用到的是LFLiveKit这个第三方框架.可以说大神们已经给我们继承好了所有的麻烦,我们只需要调用相关接口就可以了.

只是将自己踩过的坑记下来,希望众位能顺利一些。

关于LFLiveKit如何集成,这个很简单

CocoaPods

  • pod search LFLiveKit
  • pod install
  • 直接导入即可
  • 非要任性自己导入,那就看天命吧!祝你好运

#import

#import"LFLiveKit.h"

#import

  • #define CJWScreenH [UIScreen mainScreen].bounds.size.height

    #define CJWScreenW [UIScreen mainScreen].bounds.size.width

@interface start_ViewController ()

//当前区域网所在IP地址

@property (nonatomic,copy) NSString *ipAddress;

//我的房间号

@property (nonatomic,copy) NSString *myRoom;

//别人的房间号

@property (nonatomic,copy) NSString *othersRoom;

//ip后缀(如果用本地服务器,则为在nginx.conf文件中写的rtmplive)

@property (nonatomic, copy) NSString *suffix;

//大视图

@property (nonatomic,weak) UIView *bigView;

//小视图

@property (nonatomic,weak) UIView *smallView;

//播放器

//@property (nonatomic,strong)IJKFFMoviePlayerController *player;

//推流会话

@property (nonatomic,strong) LFLiveSession *session; @property(nonatomic,strong)UIView * livingPreView; @end @implementation start_ViewController

步入正题

-(void)viewDidLoad{

[super viewDidLoad];

self.livingPreView = [[UIView alloc]initWithFrame: [UIScreen mainScreen].bounds];

[self.view addSubview:self.livingPreView];

//这是一张图片,没有自己换一张

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"28258PICAJB.jpg!qt226.jpeg"]];

[self requesetAccessForVideo];

[self requesetAccessForMedio];

[self startLive];

UIButton * stopbut = [[UIButton alloc]initWithFrame:CGRectMake(20, self.view.frame.size.height-150, self.view.frame.size.width-40, 80)];

[stopbut setTitle:@"停止直播" forState:0]; stopbut.backgroundColor = [UIColor purpleColor]; stopbut.alpha = 0.3;

[self.view addSubview:stopbut];

[stopbut addTarget:self action:@selector(stopbutten) forControlEvents:UIControlEventTouchUpInside];

}

-(void)stopbutten{ [self stopLive]; [self.navigationController popViewControllerAnimated:YES];

}

-(void)viewDidAppear:(BOOL)animated{ self.tabBarController.tabBar.hidden = YES; self.navigationController.navigationBar.hidden = YES;

}

-(void)startLive{ //RTMP要设置推流地址 在这里换成你自己的网址,或是本机网址,或是服务器网址,一定要换,要不然会提示IP有误 LFLiveStreamInfo *streamInfo = [[LFLiveStreamInfo alloc]init]; streamInfo.url = [NSString stringWithFormat:@"rtmp://192.168.2.1:1935/rtmplive/room"]; [self.session startLive:streamInfo];

}

-(void)stopLive{

[self.session stopLive];

}

-(LFLiveSession *)session{ if (_session == nil) { //初始化session要传入音频配置和视频配置 //音频的默认配置为:采样率44.1 双声道 //视频默认分辨率为360 * 640 _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Low1] ]; //设置返回的视频显示在指定的view上 _session.preView = self.livingPreView; _session.delegate = self; //是否输出调试信息 _session.showDebugInfo = NO; } return _session;

}

-(void)requesetAccessForVideo{ __weak typeof(self)weakSelf = self; //判断授权状态 AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; switch (status) { case AVAuthorizationStatusNotDetermined:{ //发起授权请求 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if (granted) { dispatch_async(dispatch_get_main_queue(), ^{ //运行会话 [weakSelf.session setRunning:YES]; }); } }]; break; } case AVAuthorizationStatusAuthorized:{ //已授权则继续 dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.session setRunning:YES]; }); break; } default: break; } }

/* 请求音频资源 */

-(void)requesetAccessForMedio{ __weak typeof(self) weakSelf = self; //判断授权状态 AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; switch (status) { case AVAuthorizationStatusNotDetermined:{ //发起授权请求 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) { if (granted) { dispatch_async(dispatch_get_main_queue(), ^{ //运行会话 [weakSelf.session setRunning:YES]; }); } }]; break; } case AVAuthorizationStatusAuthorized:{ //已授权则继续 dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.session setRunning:YES]; }); break; } default: break; } }

-(void)liveSession:(nullable LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode{ //弹出警告 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"连接错误,请检查IP地址后重试" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.navigationController popViewControllerAnimated:YES]; }]; [alert addAction:sure]; [self presentViewController:alert animated:YES completion:nil]; }

IJKMediaFramework/IJKMediaFramework.h关于这个库没找到的问题,这是一个第三方播放器,网上有很多的集成帖子,自己看看就能集成,然后拉到工程中就可以了我使用它播放自己的直播,那断代码我就不发了,和推流不相关,就不跑题了.反正在推流中用不到,拉流才能用到,

下载 VLC 也可以,也可以看自己的推流.不想用就自己直接删掉吧,写上是为了方便大家查找.少踩点坑

由于本人用的是本地服务器,截图异常的丑,不不嘚瑟了

有什么问题欢迎大家留言指正,我看到一定回复.感谢!!!!!!!

简书稿源:简书 (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » iOS 推流LFLiveKit

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录