Reputation: 163
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.
Upvotes: 4
Views: 3545
Reputation: 988
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.
Upvotes: 3
Reputation: 1614
Unfortunately, it is still true for iOS 8. You can not export when app is in background but it is possible to start export as soon as it comes to foreground.
Just save the AVMutableCompostion
before your app goes to background. This can be done in applicationDidEnterBackground
. Then you can start export again in applicationDidBecomeActive
.
Note: You have to start the export all over again. It can not be resumed if not finished before entering background.
Upvotes: 2