我正在我的应用程序中录制视频,其录制时间由服务器发送。服务器测试视频中的某些手势,如果失败,则会发回一个标志,要求重新记录并重新发送。
问题:初始视频时长正确。但是在服务器“重试请求”的情况下,视频会附加黑屏一定时间,然后播放录制的视频。
流程:
- (void)viewDidLoad {
[super viewDidLoad];
// get recording time
[self getRecordingData];
}
录音功能:
-(void)startRecording {
self.isRecording = YES;
self.isProcessing = YES;
[self startWritingToVideoFile];
[self logicMethodToStopRecording];
}
写入视频文件功能
-(void)startWritingToVideoFile{
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, AVVideoCodecTypeH264, AVVideoCodecKey,nil];
self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
[self.assetWriterInput setTransform: CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 )];
self.pixelBufferAdaptor =
[[AVAssetWriterInputPixelBufferAdaptor alloc]
initWithAssetWriterInput:self.assetWriterInput
sourcePixelBufferAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA],
kCVPixelBufferPixelFormatTypeKey,
nil]];
self.videoPath = [Utilities pathForTemporaryFileWithSuffix:@"mov"];
NSURL *videoURL = [NSURL fileURLWithPath:self.videoPath];
/* Asset writer with MPEG4 format*/
self.assetWriterMyData = [[AVAssetWriter alloc]
initWithURL: videoURL
fileType:AVFileTypeMPEG4
error:nil];
[self.assetWriterMyData addInput:self.assetWriterInput];
self.assetWriterInput.expectsMediaDataInRealTime = YES;
[self.assetWriterMyData startWriting];
[self.assetWriterMyData startSessionAtSourceTime:kCMTimeZero];
self.isReadyToWrite = YES;
}
停止记录的逻辑方法:
-(void)logicMethodToStopRecording{
float time = [self.timeFromServer floatValue];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self stopRecording];
});
}
实际停止记录方法:
-(void)stopRecording{
[self stopWritingToVideoFile];
}
-(void)stopWritingToVideoFile {
[self.assetWriterMyData finishWritingWithCompletionHandler:^{
[self showLoading];
if(!self.continueRunning){
return;
}
[self sendVideo];
// sends video file referenced in startWritingToVideoFile method
}];
}
发送视频的方法:
-(void)sendVideo{
[self.xxxxxx sendAPI:self.userToVerifyUserId videoPath:self.videoPath callback:^(NSString *jsonResponse){
[self handleResponse:jsonResponse];
}];
}
通过以下方法处理响应:
-(void)handleResponse: (NSString*)result{
[Utilities deleteFile:self.videoPath];
//if no success but retry is true. re-record and re-send.
if(!self.success && retry){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self startRecording]; // start recording called again
});
}
我创建的初始文件假设有 x秒的视频----它正在正确生成
但是,在重新尝试期间:
我要删除旧文件并重新创建新文件。在第二次和第三次尝试中,我的视频输出仍然黑屏了 n + x和2n + x 次。请帮忙。
**************编辑************************************ ********************
另外,AVCaptureSession的回调方法执行以下操作:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// a very dense way to keep track of the time at which this frame
// occurs relative to the output stream, but it's just an example!
static int64_t frameNumber = 0;
if(self.assetWriterInput.readyForMoreMediaData){
if(self.pixelBufferAdaptor != nil){
[self.pixelBufferAdaptor appendPixelBuffer:imageBuffer
withPresentationTime:CMTimeMake(frameNumber, 25)];
frameNumber++;
}
}
}
}
**************************编辑-2 ****************** **************************** p
+(NSString *)pathForTemporaryFileWithSuffix:(NSString *)suffix
{
NSString * result;
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
assert(uuidStr != NULL);
result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", uuidStr, suffix]];
assert(result != nil);
CFRelease(uuidStr);
CFRelease(uuid);
return result;
}