Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone iOS : [AVAssetWriterInput appendSampleBuffer:] Cannot call method when status is 0

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..

like image 877
Zigglzworth Avatar asked Nov 09 '22 23:11

Zigglzworth


1 Answers

Did you add the assetwriterinput to the assetwriter? normally you should do that in the viewdidload method.

[self.assetWriter addInput:self.assetWriterInput];
like image 94
Ramin Afshar Avatar answered Nov 14 '22 23:11

Ramin Afshar