Search code examples
iphoneiosobjective-cavassetwriter

AVAssetWriter fails once App goes in background


I am working on iOS app in which i am creating video from images. I am using AVAssetWriter to achieve this. Everything works fine. But When app goes in background and switched back, the video writing fails. AVAssetWriter's finishWritingWithCompletionHandler is not getting called when i switch back to app.

May be duplicate of AVAssetWriter fails when Application enters Background during Rendering , but i am not getting any help from there.

Any idea for this?

Thanks


Solution

  • This answer is based on the assumption that you want the video to continue rendering while in the background.

    I fixed this in my app doing by asking the OS to grant the app background task permisions (for a limited amount of time). This answer might help you too iOS generating a video in the background task

        @property (nonatomic,assign) UIBackgroundTaskIdentifier __block backgroundRenderingID;
    
        UIApplication *app = [UIApplication sharedApplication];
    
        _backgroundRenderingID = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:_backgroundRenderingID];
            _backgroundRenderingID = UIBackgroundTaskInvalid;
        }];
    

    Don't forget to let go once you are done!

    [[UIApplication sharedApplication] endBackgroundTask:_backgroundRenderingID];