一审孽、nginx + rtmp +ffmpeg服務器安裝教程
地址:http://www.reibang.com/p/b1207e41edaf
二薇溃、LFLiveKit庫(推流)
使用Cocoapod導入LFLiveKit庫
三肴掷、IJKMediaPlayer庫集成教程(拉流)
地址:http://www.reibang.com/p/82f435ae21cf
四:配置手機網絡
1夹抗、配置好nginx服務器的Mac和ipone連接同一個wifi
2俊马、查看Mac當前網絡IP
3、進入手機當前網絡的配置代理敷存,將服務器ip設置為Mac的ip墓造,端口為8080
五:代碼實現
推流
#import <LFLiveKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RecordingViewController ()<LFLiveSessionDelegate>
@property (nonatomic,strong) LFLiveSession *session;
@end
@implementation RecordingViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self beatutyfaceBtn];
//錄制端
[self requestAccessForVidoe];
[self requestAudio];
[self startLive];
}
- (void)beatutyfaceBtn{
UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
actionBtn.backgroundColor = [UIColor redColor];
[actionBtn setTitle:@"開始直播" forState:UIControlStateNormal];
[actionBtn addTarget:self action:@selector(openBeatyface:) forControlEvents:UIControlEventTouchUpInside];
actionBtn.frame = CGRectMake(self.view.center.x - 50, 400, 100, 50);
[self.view addSubview:actionBtn];
}
- (LFLiveSession*)session {
if (!_session) {
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
_session.preView = self.view;
_session.showDebugInfo = YES;
_session.delegate = self;
}
return _session;
}
//獲取系統(tǒng)的攝像頭權限,獲取視屏資源:
- (void)requestAccessForVidoe{
__weak typeof (self) weakSelf = self;
//判斷授權狀態(tài)
AVAuthorizationStatus statues = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (statues) {
case AVAuthorizationStatusNotDetermined:{
//發(fā)起授權請求
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
//進行會話
[weakSelf.session setRunning:YES];
});
}
}];
break;
}
case AVAuthorizationStatusAuthorized:{
//已授權繼續(xù)
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.session setRunning:YES];
});
break;
}
default:
break;
}
}
//獲取音頻權限與資源:
- (void)requestAudio
{
__weak typeof (self) weakSelf = self;
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (status) {
case AVAuthorizationStatusNotDetermined:{
//發(fā)起授權請求
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
//進行會話
[weakSelf.session setRunning:YES];
});
}
}];
break;
}
case AVAuthorizationStatusAuthorized:{
//授權繼續(xù)
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.session setRunning:YES];
});
}
default:
break;
}
}
//LFLivekit監(jiān)聽delegate方法:
- (void)liveSession:(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];
}
- (void)startLive {
LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
streamInfo.url = @"rtmp://192.168.1.121:1935/rtmplive/room";
[self.session startLive:streamInfo];
}
- (void)openBeatyface:(UIButton *)btn{
_session.beautyFace = YES;
}
- (void)stopLive {
[self.session stopLive];
}
//MARK: - CallBack:
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange: (LFLiveState)state{
}
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug*)debugInfo{
}
- (void)viewDidDisappear:(BOOL)animated{
[self stopLive];
}
@end
拉流
#import <IJKMediaFramework/IJKFFMoviePlayerController.h>
@interface PlayLiveViewController ()
@property(nonatomic,strong)IJKFFMoviePlayerController * player;
@end
@implementation PlayLiveViewController
- (void)viewDidLoad {
[super viewDidLoad];
IJKFFOptions *options = [IJKFFOptions optionsByDefault]; //使用默認配置
NSURL * url = [NSURL URLWithString:@"rtmp://192.168.1.121:1935/rtmplive/room"];
self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.player.view.frame = self.view.bounds;
self.player.scalingMode = IJKMPMovieScalingModeAspectFit;
self.player.shouldAutoplay = YES;
self.view.autoresizesSubviews = YES;
[self.view addSubview:self.player.view];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.player prepareToPlay];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.player shutdown];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
rtmp://192.168.1.121:1935/rtmplive/room
192.168.1.121:nginx 所在Mac的當前網絡ip
注意事項:
plist添加
Privacy - Camera Usage Description 相機
Privacy - Microphone Usage Description 錄音
Privacy - Photo Library Usage Description 相冊
簡單寫了一個實現簡易直播效果的demo
地址:https://gitee.com/xu_sheng_jie/live-demo.git