基于之前搭建的nginx + rtmp服务器,现在结合LFLivekit,编码获取摄像头视屏,麦克风音频编码推流
首先先将需要准备的东西下载下来集成到你的工程中去
在github上下载LFLivekit集成到你的工程中去
需要在工程中添加相应的framework-
- UIKit
- Foundation
- AVFoundation
- VideoToolbox
- AudioToolbox
- libz
- libstdc++
-
下载ijkPlayer,这个播放器现在好像直播的app大都有的这个,也是基于FFMpeg封装的,也可以直接用FFMPeg +kxMovie 做播放器,FFMPeg + kxmovie 播放器的编译方法我在另一篇中有写过FFMPeg + kxmovie编译ios平台下使用 ijkplayer编译方法还没来的急记录下来,下次写了再来这里补充,我这里直接提供一个下载编译好的IJKMediaFramework.framWork加入工程就可以使用
下面直接贴代码编码代码
创建一个控制器继承自viewController
导入库
#import <IJKMediaFramework/IJKMediaFramework.h>
#import "LFLiveKit.h"
在LFLivekit中,LFLiveSession 是一个主要用的类
在inteface 中声明称全局的量
@property (nonatomic ,strong)LFLiveSession *session;
使用setter方法
-(LFLiveSession *)session
{
if (_session == nil) {
//初始化session要传入音频配置和视频配置
//音频的默认配置为:采样率44.1 双声道
//视频默认分辨率为360 * 640
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Low1]];
_session.preView = self.SmallView;
_session.delegate = self;
//是否输出调试信息
_session.showDebugInfo = YES;
}
return _session;
}
首先获取系统的摄像头权限,获取视屏资源
- (void)requestAccessForVideo
{
__weak typeof (self) weakSelf = self;
//判断授权状态
AVAuthorizationStatus statues = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (statues) {
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)requestAudio
{
__weak typeof (self) weakSelf = self;
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
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];
});
}
default:
break;
}
}
LFLivekit监听delegate方法
#pragma mark ===session的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
{
//RTMP要设置推流地址
LFLiveStreamInfo *stremInfo = [LFLiveStreamInfo new];
stremInfo.url = @"rtmp://192.168.1.173:1935/rtmplive/room";
[self.session startLive:stremInfo];
}
//这里的@"rtmp://192.168.1.173:1935/rtmplive/room";
这个地址是我自己建立的nginx + rtmp本地的推流服务器,在上一篇的block中有具体的介绍,这里就不在说了其中192.168.1.173 是自己的电脑ip,打开网络偏好设置即可查看的到注意替换正确,不然推流会失败
在viewdidload中调用就可以打开摄像头进行推流了,注意在plist文件中添加摄像头和麦克风的授权请求
- (void)viewDidLoad {
[super viewDidLoad];
//录制端
[self requestAccessForVideo];
[self requestAudio];
[self startLive];
}
在播放的也页面可以使用ijk 可以拉流
拉流的方法为
-(IJKFFMoviePlayerController *)player
{
if (_player == nil) {
IJKFFOptions *options = [IJKFFOptions optionsByDefault];
_player = [[IJKFFMoviePlayerController alloc]initWithContentURLString:@"rtmp://192.168.1.173:1935/rtmplive/room" withOptions:options];
//设置填充模式
_player.scalingMode = IJKMPMovieScalingModeAspectFill;
//设置播放视图
_player.view.frame = self.bigView.bounds;
[self.bigView addSubview:_SmallView];
//设置自动播放
_player.shouldAutoplay = YES;
[_player prepareToPlay];
}
return _player;
}