IOS开发:一对一直播系统中视频录制切换前后摄像头后方向错误解决办法

2019-03-05 14:38:03 浏览数 (1)

最近发现项目里,切换前后摄像头几次之后,会出现录制的视频方向上下颠倒,可是明明已经在切换摄像头后,给videoconnection重新设置了录制方向,我项目视频录制固定为横屏录制,所以每次切换摄像头都执行了以下方法self.videoConnection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;摸索了两天,才发现了bug所在,是因为videoConnection 采用了懒加载,切换摄像头后并没有重新初始化,所以才让设置没有生效。解决办法很简单,重写懒加载,直接改为每次都重新初始化。

代码语言:javascript复制
//视频连接

- (AVCaptureConnection*)videoConnection {
_videoConnection= [self.videoOutputconnectionWithMediaType:AVMediaTypeVideo];
return_videoConnection;
}
最后附上摄像头前后切换的代码
//切换前后置摄像头

- (void)changeCameraInputDeviceisFront:(BOOL)isFront {
if(isFront) {
//改变会话的配置前一定要先开启配置,配置完成后提交配置改变
[self.recordSessionstopRunning];
[self.recordSessionbeginConfiguration];
[self.recordSessionremoveInput:self.backCameraInput];
if([self.recordSessioncanAddInput:self.frontCameraInput]) {
[selfchangeCameraAnimation];
[self.recordSessionaddInput:self.frontCameraInput];
//提交会话配置
[self.recordSessioncommitConfiguration];
}
}else{
[self.recordSessionstopRunning];
[self.recordSessionbeginConfiguration];
[self.recordSessionremoveInput:self.frontCameraInput];
if([self.recordSessioncanAddInput:self.backCameraInput]) {
[selfchangeCameraAnimation];
[self.recordSessionaddInput:self.backCameraInput];
[self.recordSessioncommitConfiguration];
}}}
//后置摄像头输入

- (AVCaptureDeviceInput*)backCameraInput {
if(_backCameraInput==nil) {
NSError*error;
_backCameraInput= [[AVCaptureDeviceInputalloc]initWithDevice:[selfbackCamera]error:&error];
if(error) {
NSLog(@"获取后置摄像头失败~");
}
}
return_backCameraInput;
}
//前置摄像头输入
- (AVCaptureDeviceInput*)frontCameraInput {
if(_frontCameraInput==nil) {
NSError*error;
_frontCameraInput= [[AVCaptureDeviceInputalloc]initWithDevice:[selffrontCamera]error:&error];
if(error) {
NSLog(@"获取前置摄像头失败~");
}
}
return_frontCameraInput;
}

0 人点赞