@interface FKMainViewController ()
/// 记录拼接URL
@property (nonatomic,copy) NSURL *streamCloudURL;
/// 记录从服务器请求回来URL
@property (nonatomic,copy) NSURL *streamURL;
/// 推流中的核心类
@property (nonatomic,strong) PLMediaStreamingSession *session;
/// 摄像头放大缩小
@property (nonatomic,strong) UISlider *zoomSlider;
@end
@implementationFKMainViewController
/// 销毁推流中核心类
- (void)viewDidDisappear:(BOOL)animated {
[superviewDidDisappear:animated];
[self.sessiondestroy];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 记录推流地址
[self getStreamCloudURL];
// 从服务器获取推流地址
[self generateStreamURLFromServerWithURL:self.streamCloudURL];
// 设置相机
[self prepareForCameraSetting];
// 设置反摄像头
[self pressedChangeCameraButton];
}
/// 在这里填写获取推流地址的业务服务器URL
- (void)getStreamCloudURL {
// NSString *streamServer = @"your app server URL";
NSString*streamServer =@"";
self.streamCloudURL= [NSURLURLWithString:streamServer];
}
/// 使用URL从服务器生成流URL
- (void)generateStreamURLFromServerWithURL:(NSURL*)url {
[selfgetRequest:url];
}
- (void)getRequest:(NSURL*)url {
NSDictionary*dict =@{
@"code":@"9-18点开播",
@"description":@"聊天室描述",
@"maxusers":@"1000",
@"name":@"聊天室名称",
@"owner":@"15622731953",// 聊天室账号
@"playName":@"叶君臣"
};
__weak typeof(self)weakSelf = self;
[NetworkToolspostRequestWithURL:@"room/getPiliAddress"Parameter:dictCompletionHandler:^(id _NonnullresponseObj,NSError*_Nonnullerror) {
if(!error) {
NSString*code = [NSStringstringWithFormat:@"%@",responseObj[@"code"]];
if([codeisEqualToString:@"200"]) {
NSLog(@"data = %@",responseObj[@"data"]);
// 更新推流的URL
weakSelf.streamURL= [NSURLURLWithString:[NSStringstringWithFormat:@"%@",responseObj[@"data"][@"url"]]];
[weakSelf.sessionstartStreamingWithPushURL:weakSelf.streamURLfeedback:^(PLStreamStartStateFeedbackfeedback) {
if(PLStreamStartStateSuccess== feedback) {
NSLog(@"推流成功");
}else{
NSLog(@"推流失败");
}
}];
}else{
}
}else{
}
}];
}
/// 设置相机
- (void)prepareForCameraSetting {
/// 视频采集配置
PLVideoCaptureConfiguration *videoCaptureConfiguration = [PLVideoCaptureConfiguration defaultConfiguration];
/// 音频采集配置
PLAudioCaptureConfiguration *audioCaptureConfiguration = [PLAudioCaptureConfiguration defaultConfiguration];
/// 视频编码配置
PLVideoStreamingConfiguration *videoStreamingConfiguration = [PLVideoStreamingConfiguration defaultConfiguration];
/// 音频编码配置
PLAudioStreamingConfiguration *audioStreamingConfiguration = [PLAudioStreamingConfiguration defaultConfiguration];
self.session= [[PLMediaStreamingSessionalloc]initWithVideoCaptureConfiguration:videoCaptureConfigurationaudioCaptureConfiguration:audioCaptureConfigurationvideoStreamingConfiguration:videoStreamingConfigurationaudioStreamingConfiguration:audioStreamingConfigurationstream:nil];
self.session.delegate = self;
[self.view addSubview:self.session.previewView];
}
/// 设置反摄像头
- (void)pressedChangeCameraButton {
UIButton*changeCameraBtn = [[UIButtonalloc]init];
changeCameraBtn.frame=CGRectMake(self.view.bounds.size.width-50,84,30,30);
[changeCameraBtnsetTitle:@"转" forState:UIControlStateNormal];
[changeCameraBtnsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[self.viewaddSubview:changeCameraBtn];
[changeCameraBtnaddTarget:self action:@selector(changeCameraClick) forControlEvents:UIControlEventTouchUpInside];
// 设置滑动放大放小
self.zoomSlider= [[UISlideralloc]init];
self.zoomSlider.frame = CGRectMake(30, self.view.bounds.size.height-100, self.view.bounds.size.width-60, 30);
self.zoomSlider.value = 1.0;
self.zoomSlider.minimumValue = 1.0;
self.zoomSlider.maximumValue = MIN(5, self.session.videoActiveFormat.videoMaxZoomFactor);
[self.viewaddSubview:self.zoomSlider];
[self.zoomSlider addTarget:self action:@selector(scrollSlider:) forControlEvents:UIControlEventValueChanged];
}
/// 切换摄像头
- (void)changeCameraClick {
[self.session toggleCamera];
self.zoomSlider.minimumValue = 1.0;
self.zoomSlider.maximumValue = MIN(5, self.session.videoActiveFormat.videoMaxZoomFactor);
}
/// 放大缩小
- (void)scrollSlider:(UISlider*)slider {
self.session.videoZoomFactor = slider.value;
}