Skip to content

Commit

Permalink
FIXED - fix video's orientation to portrait
Browse files Browse the repository at this point in the history
  • Loading branch information
SummerHaze committed Mar 3, 2017
1 parent 2084bb3 commit baa4c00
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 158 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
startingLineNumber = "1001"
endingLineNumber = "1001"
landmarkName = "-executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:"
landmarkType = "5">
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
Expand All @@ -48,7 +48,7 @@
startingLineNumber = "993"
endingLineNumber = "993"
landmarkName = "-executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:"
landmarkType = "5">
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
Expand Down
164 changes: 57 additions & 107 deletions IGuess/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions IGuess/UIImage+WaterMark.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

+ (UIImage *)imageWithimage:(UIImage *)image content:(NSString *)content frame:(CGRect)frame;

@property (nonatomic, copy) NSString *str;

@end
1 change: 1 addition & 0 deletions IGuess/XZCardView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@protocol XZCardViewDelegate <NSObject>

- (void)showMeaningWebview:(XZCardView *)cardView;
@property (nonatomic, copy) NSString *str;

@end

Expand Down
6 changes: 3 additions & 3 deletions IGuess/XZPlayViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ - (void)updateGameCountDown {
// 游戏结束强制弹框
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:@"游戏结束"
message:[NSString stringWithFormat:@"PASS: %ld\nFAIL: %ld",(long)passCount, (long)failCount]
// 视频录制功能暂时不开放
// message:[NSString stringWithFormat:@"PASS: %ld\n FAIL: %ld\n 如有录制视频,请在本地相册查看",(long)passCount, (long)failCount]
// message:[NSString stringWithFormat:@"PASS: %ld\nFAIL: %ld",(long)passCount, (long)failCount]
// 视频录制功能暂时不开放
message:[NSString stringWithFormat:@"PASS: %ld\n FAIL: %ld\n 如有录制视频,请在本地相册查看",(long)passCount, (long)failCount]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"再来一轮"
style:UIAlertActionStyleCancel
Expand Down
8 changes: 4 additions & 4 deletions IGuess/XZVideoEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ - (void)dealloc {
_path = nil;
}

//WCLRecordEncoder遍历构造器的
//XZRecordEncoder遍历构造器的
+ (XZVideoEncoder*)encoderForPath:(NSString*)path Height:(NSInteger)cy width:(NSInteger)cx channels:(int)ch samples:(Float64)rate
{
XZVideoEncoder *enc = [XZVideoEncoder alloc];
return [enc initPath:path Height:cy width:cx channels:ch samples:rate];
XZVideoEncoder *enc = [[XZVideoEncoder alloc]initPath:path Height:cy width:cx channels:ch samples:rate];
return enc;
}

//初始化方法
Expand Down Expand Up @@ -60,7 +60,7 @@ - (instancetype)initPath:(NSString*)path Height:(NSInteger)cy width:(NSInteger)c
//初始化视频输入
- (void)initVideoInputHeight:(NSInteger)cy width:(NSInteger)cx {
//录制视频的一些配置,分辨率,编码方式等等
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger: cx], AVVideoWidthKey,
[NSNumber numberWithInteger: cy], AVVideoHeightKey,
Expand Down
92 changes: 51 additions & 41 deletions IGuess/XZVideoEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ @interface XZVideoEngine() <AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptu

@implementation XZVideoEngine

#pragma mark - public方法
#pragma mark - public方法,管理视频录制进度
//启动录制
- (void)startUp {
// NSLog(@"启动录制功能");
self.startTime = CMTimeMake(0, 0);
self.isCapturing = NO;
self.isPaused = NO;
Expand All @@ -71,7 +70,6 @@ - (void)shutdown {
- (void) startCapture {
@synchronized(self) {
if (!self.isCapturing) {
// NSLog(@"开始录制");
self.videoEncoder = nil;
self.isPaused = NO;
self.isDisconnected = NO;
Expand Down Expand Up @@ -171,6 +169,7 @@ - (void)stopCaptureHandler:(void (^)(UIImage *movieImage))handler {
// [NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:generatorHandler];
//}

#pragma mark - getter
//捕获到的视频呈现的layer
- (AVCaptureVideoPreviewLayer *)previewLayer {
if (_previewLayer == nil) {
Expand Down Expand Up @@ -200,21 +199,27 @@ - (AVCaptureSession *)recordSession {
[_recordSession addOutput:self.videoOutput];
//设置视频的分辨率
// 此处设置的分辨率,影响视频输出流的尺寸
_cx = 1280;
_cy = 720;
_cx = 720;
_cy = 1280;
}
//添加音频输出
if ([_recordSession canAddOutput:self.audioOutput]) {
[_recordSession addOutput:self.audioOutput];
}
//设置视频录制的方向
DDLogDebug(@"support video orientation : %d", self.videoConnection.isVideoOrientationSupported);
self.videoConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
}
return _recordSession;
}

#pragma mark - AVFoundation input
//获取videoSession的videoConnection对象,用于管理视频方向等——解决了录制的视频是横向的问题
- (AVCaptureConnection *)videoConnection {
if (_videoConnection == nil) {
_videoConnection = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo];
}
return _videoConnection;
}

//后置摄像头输入
- (AVCaptureDeviceInput *)backCameraInput {
if (_backCameraInput == nil) {
Expand Down Expand Up @@ -252,17 +257,17 @@ - (AVCaptureDeviceInput *)audioMicInput {
return _audioMicInput;
}

#pragma mark - AVFoundation output
//视频输出
- (AVCaptureVideoDataOutput *)videoOutput {
if (_videoOutput == nil) {
_videoOutput = [[AVCaptureVideoDataOutput alloc] init];
// 设置delegate,在captureQueue中调用captureOutput获取frame。queue必须是serial queue,保证frame的有序传递
[_videoOutput setSampleBufferDelegate:self queue:self.captureQueue];
// 设置硬件解码器输出格式,提高转码效率
NSDictionary* capSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange],kCVPixelBufferPixelFormatTypeKey,
nil];
NSDictionary *capSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange],
kCVPixelBufferPixelFormatTypeKey,
nil];
_videoOutput.videoSettings = capSettings;
}
return _videoOutput;
Expand Down Expand Up @@ -307,8 +312,9 @@ - (AVCaptureDevice *)backCamera {
// }
//}

//用来返回是前置摄像头还是后置摄像头
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {

//返回是前置摄像头还是后置摄像头
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position {
//返回和视频录制相关的所有默认设备
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
//遍历这些设备返回跟position相关的设备
Expand All @@ -320,26 +326,26 @@ - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
return nil;
}

//开启闪光灯
- (void)openFlashLight {
AVCaptureDevice *backCamera = [self backCamera];
if (backCamera.torchMode == AVCaptureTorchModeOff) {
[backCamera lockForConfiguration:nil];
backCamera.torchMode = AVCaptureTorchModeOn;
backCamera.flashMode = AVCaptureFlashModeOn;
[backCamera unlockForConfiguration];
}
}
//关闭闪光灯
- (void)closeFlashLight {
AVCaptureDevice *backCamera = [self backCamera];
if (backCamera.torchMode == AVCaptureTorchModeOn) {
[backCamera lockForConfiguration:nil];
backCamera.torchMode = AVCaptureTorchModeOff;
backCamera.flashMode = AVCaptureTorchModeOff;
[backCamera unlockForConfiguration];
}
}
////开启闪光灯
//- (void)openFlashLight {
// AVCaptureDevice *backCamera = [self backCamera];
// if (backCamera.torchMode == AVCaptureTorchModeOff) {
// [backCamera lockForConfiguration:nil];
// backCamera.torchMode = AVCaptureTorchModeOn;
// backCamera.flashMode = AVCaptureFlashModeOn;
// [backCamera unlockForConfiguration];
// }
//}
////关闭闪光灯
//- (void)closeFlashLight {
// AVCaptureDevice *backCamera = [self backCamera];
// if (backCamera.torchMode == AVCaptureTorchModeOn) {
// [backCamera lockForConfiguration:nil];
// backCamera.torchMode = AVCaptureTorchModeOff;
// backCamera.flashMode = AVCaptureTorchModeOff;
// [backCamera unlockForConfiguration];
// }
//}

//录制的队列
- (dispatch_queue_t)captureQueue {
Expand All @@ -350,8 +356,9 @@ - (dispatch_queue_t)captureQueue {
}


#pragma mark - 存储视频
#pragma mark - AVCaptureAudioDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

BOOL isVideo = YES;
@synchronized(self) {
if (!self.isCapturing || self.isPaused) {
Expand All @@ -360,6 +367,7 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
if (captureOutput != self.videoOutput) {
isVideo = NO;
}

//初始化编码器,当有音频和视频参数时创建编码器
if ((self.videoEncoder == nil) && !isVideo) {
CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sampleBuffer);
Expand Down Expand Up @@ -392,13 +400,15 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
// _lastVideo.flags = 0;
// _lastAudio.flags = 0;
// }

// 增加sampleBuffer的引用计时,这样我们可以释放这个或修改这个数据,防止在修改时被释放
CFRetain(sampleBuffer);
if (_timeOffset.value > 0) {
CFRelease(sampleBuffer);
//根据得到的timeOffset调整
sampleBuffer = [self adjustTime:sampleBuffer by:_timeOffset];
}
// if (_timeOffset.value > 0) {
// CFRelease(sampleBuffer);
// //根据得到的timeOffset调整
// sampleBuffer = [self adjustTime:sampleBuffer by:_timeOffset];
// }

// // 记录暂停上一次录制的时间
// CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
// CMTime dur = CMSampleBufferGetDuration(sampleBuffer);
Expand Down Expand Up @@ -451,7 +461,7 @@ - (NSString *)getVideoCacheDir {
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];

// summ 很奇怪,为什么要判断videos下面是否有文件?先注释掉
// 为什么要判断videos下面是否有文件?先注释掉
// // 该existed仅能表示videoCacheDir中是否有文件或文件夹,路径本身是否是文件夹的判断结果,存储在isDir中
// BOOL existed = [fileManager fileExistsAtPath:videoCacheDir isDirectory:&isDir];
// // 本身不是文件夹,或目录下没有文件和文件夹,则创建videos文件夹
Expand Down Expand Up @@ -498,7 +508,7 @@ - (CMSampleBufferRef)adjustTime:(CMSampleBufferRef)sample by:(CMTime)offset {
return sout;
}

#pragma mark - 切换动画
// 切换动画
//- (void)changeCameraAnimation {
// CATransition *changeAnimation = [CATransition animation];
// changeAnimation.delegate = self;
Expand Down
2 changes: 1 addition & 1 deletion IGuess/XZWordGuessingGame.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ - (void)saveResultsToDB{
- (void)saveSingleResult:(NSString *)name result:(NSString *)result {
// 要用毫秒时间戳形式存储游戏时间,否则点击过快可能造成两次的时间相同,添加进字典失败
NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval interval = [dat timeIntervalSince1970]*1000;
NSTimeInterval interval = [dat timeIntervalSince1970] * 1000;
NSString *timestamp = [NSString stringWithFormat:@"%.0f", interval];

XZResultDetailItem *item = [[XZResultDetailItem alloc]init];
Expand Down

0 comments on commit baa4c00

Please sign in to comment.