I am trying to append CMSampleBufferRefs to an AVAssetWriterInput and I keep getting a crash with error:
[AVAssetWriterInput appendSampleBuffer:] Cannot call method when status is 0
Code:
in 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;
Record functions
-(void)prepareVideo {
if (![assetWriter startWriting]) {
NSLog(@"%li, %@", assetWriter.status, assetWriter.error.localizedDescription);
}
}
-(void)recordVideo {
recordingVideo = YES;
[assetWriter startSessionAtSourceTime:kCMTimeZero];
}
delegate
- (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];
}
}
}
}
The status of the AVAssetWriter is 1 so I know this is not the issue..
Did you add the assetwriterinput to the assetwriter? normally you should do that in the viewdidload method.
[self.assetWriter addInput:self.assetWriterInput];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With