Error starting AVAssetWriter while running in the background

I am trying to save images to a video file while the application is in the background

I am trying to create an AVAssetWriter and start writing, but this operation fails (returns NO).
Here are some of the checks I have performed:

  • The file does not exist
  • I call [[UIApplication sharedApplication] beginBackgroundTaskWithName: expirationHandler:]

    before running my code
  • Activator asset status prior to calling startWriting AVAssetWriterStatusUnknown

The error I receive (in the code below videoWriter.error

):
Domain Error = AVFoundationErrorDomain Code = -11800 "The operation could not be completed" UserInfo = 0x17e1e710 {NSLocalizedDescription = The operation could not be completed, NSUnderlyingError = 0x17ec85d0 "The operation could not be completed. (Error OSStatus -16980.) ", NSLocalizedFailureReason = An unknown error occurred (-16980)}

And to be specific about the underlying error: Error Domain = NSOSStatusErrorDomain Code = -16980 "The operation could not be performed. (OSStatus error -16980.)

I couldn't find any explanation for this behavior or error code

Please advise!

- (BOOL)_createAssetWriterWithFrameSize:(CGSize)frameSize filePath:(NSString *)filePath{
       NSURL *filePathURL = [NSURL fileURLWithPath:filePath];

        NSError *error = nil;

        AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:filePathURL
                                                               fileType:AVFileTypeQuickTimeMovie
                                                                  error:&error];
        if(error!=nil){
            LogDebug(@"Error creating %@", error);
        }

        if (videoWriter) {

            NSDictionary *videoSettings = @{AVVideoCodecKey: AVVideoCodecH264,
                                            AVVideoWidthKey: @(frameSize.width),
                                            AVVideoHeightKey: @(frameSize.height),//};

                                            };

            AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                                                 outputSettings:videoSettings];

            NSDictionary *attributes = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32ARGB),
                                         (NSString *)kCVPixelBufferWidthKey: @(frameSize.width),
                                         (NSString *)kCVPixelBufferHeightKey: @(frameSize.height)};


            AVAssetWriterInputPixelBufferAdaptor *adaptor = nil;
            if(attributes!=nil){
                adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                                                                                    sourcePixelBufferAttributes:attributes];
            }
            if ([videoWriter canAddInput:writerInput]) {
                [videoWriter addInput:writerInput];

                writerInput.expectsMediaDataInRealTime = YES;


                BOOL start = [videoWriter startWriting];

                if (start) {
                    [videoWriter startSessionAtSourceTime:kCMTimeZero];

                }
                else{

                    LogInfo(@"Could not start writing to video file due to %@. file is %@", videoWriter.error, filePath);
                    id o = [videoWriter.error.userInfo objectForKey:NSUnderlyingErrorKey];
                    if(o!=nil){
                        LogDebug(@"Failed writing, underlying error is %@", o);
                    }

                }
            }
        }
}

      

+3


source to share





All Articles