Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting videos with AVAssetExportSession in background the background

I am working on an iOS 8 app which records a video, modifies it using AVMutableVideoComposition and exports the result with a AVAssetExportSession calling exportAsynchronouslyWithCompletionHandler.

Things work fine as long as the app is in the foreground. When a user pushes the home button and sends the app into the background before the export has finished however, the following error is raised:

Error Domain=AVFoundationErrorDomain Code=-11847 "Operation Interrupted"     
UserInfo=0x174271c40 {NSUnderlyingError=0x170246c90 "The operation couldn't 
be completed. (OSStatus error -12125.)", NSLocalizedRecoverySuggestion=Stop 
other operations and try again., NSLocalizedDescription=Operation 
Interrupted}

It looks like others have experienced the same error with Audio: AVAssetExportSession working in background

I have tried all kind of things like requesting more time for the execution using beginBackgroundTaskWithExpirationHandler (does not work because the export is interrupted before the time runs out), adding Audio as a Background Mode in the app capabilities (does not work, maybe because I use AVMutableVideoComposition?), starting the export in a custom queue outside the main thread, but nothing works.

Even trying to restart the export in case it has not completed when the app enters background fails. I tried to listen to UIApplicationDidEnterBackgroundNotification and restart the export after the app is already in the background but I get the same error.

The discussion in this thread hints that it could be because AVMutableVideoComposition can not be used in the background:

Starting AVAssetExportSession in the Background

Is this still true for iOS 8? Is there any workaround or does anyone know how I could accomplish the desired functionality (editing and exporting a video a user has just recorded even if he sends the app to background)?

Any help is highly appreciated, I have spent many hours on this problem.

like image 718
Andi S. Avatar asked Oct 21 '25 14:10

Andi S.


1 Answers

iOS 13 tested. Play silent sound while exporting.

Audio session should be configured like:

try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
try AVAudioSession.sharedInstance().setActive(true)

Background mode required.

like image 99
Максим Котляр Avatar answered Oct 23 '25 03:10

Максим Котляр



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!