4

I am trying to save images into a video file, while the app is in the background

I try to create the AVAssetWriter and start writing but this operation fails (returns NO).
Here are some checks I performed:

  • The file does not exist
  • I invoke [[UIApplication sharedApplication] beginBackgroundTaskWithName: expirationHandler:] prior to running my code
  • The status of the assetWriter prior to calling startWriting is AVAssetWriterStatusUnknown

The Error I receive (in the code below videoWriter.error) is :
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x17e1e710 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x17ec85d0 "The operation couldn’t be completed. (OSStatus error -16980.)", NSLocalizedFailureReason=An unknown error occurred (-16980)}

And to be specific on the underlying error: Error Domain=NSOSStatusErrorDomain Code=-16980 "The operation couldn’t be completed. (OSStatus error -16980.)

I couldn't find any explanation to this behaviour 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
  • 1
    how did u fixed that plz? May 12, 2015 at 9:07
  • unfortunately I had to workaround this.
    – Nech
    May 12, 2015 at 12:15
  • Did you find any solution for this? Oct 27, 2015 at 9:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.