我正在尝试将CMSampleBufferRefs附加到AVAssetWriterInput上,并且不断出现崩溃并出现错误:



代码:

在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,所以我知道这不是问题。

最佳答案

您是否将avassetwriter输入添加到 Assets 写作者?
通常,您应该在viewdidload方法中执行此操作。

[self.assetWriter addInput:self.assetWriterInput];

关于ios - iphone iOS : [AVAssetWriterInput appendSampleBuffer:] Cannot call method when status is 0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26592610/

10-09 02:12