我正在尝试将CMSampleBufferRefs附加到AVAssetWriterInput,并且因错误而一直崩溃:
[AVAssetWriterInput appendSampleBuffer:]状态为0时无法调用方法
代码:
在viewDidLoad
中NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [cachePaths firstObject];
NSString *filename = @"test.mp4";
NSString *filePath = [cacheDirectory stringByAppendingPathComponent:filename];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:filePath];
NSError *errors;
assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:(NSString *)kUTTypeMPEG4 error:&errors];
videoWriteInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
audioWriteInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:audioSettings];
[audioWriteInput addTrackAssociationWithTrackOfInput:videoWriteInput type:AVTrackAssociationTypeTimecode];
audioWriteInput.expectsMediaDataInRealTime = YES;
videoWriteInput.expectsMediaDataInRealTime = YES;
记录功能
-(void)prepareVideo {
if (![assetWriter startWriting]) {
NSLog(@"%li, %@", assetWriter.status, assetWriter.error.localizedDescription);
}
}
-(void)recordVideo {
recordingVideo = YES;
[assetWriter startSessionAtSourceTime:kCMTimeZero];
}
委托
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if (recordingVideo) {
if ([captureOutput isKindOfClass:[AVCaptureVideoDataOutput class]]) {
if (assetWriter.status != 0) {
[videoWriteInput appendSampleBuffer:sampleBuffer];
}
}
else if ([captureOutput isKindOfClass:[AVCaptureAudioDataOutput class]]) {
if (assetWriter.status != 0) {
[audioWriteInput appendSampleBuffer:sampleBuffer];
}
}
}
}
AVAssetWriter的状态是1,所以我知道这不是问题..
答案 0 :(得分:0)
您是否已将avassetwriterinput添加到资产撰写人? 通常你应该在viewdidload方法中这样做。
[self.assetWriter addInput:self.assetWriterInput];