简体   繁体   English

在后台使用 AVAssetExportSession 导出视频

[英]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 .我正在开发一个 iOS 8 应用程序,它记录视频,使用AVMutableVideoComposition修改它并使用调用exportAsynchronouslyWithCompletionHandlerAVAssetExportSession导出结果。

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看起来其他人在使用 Audio: AVAssetExportSession 时遇到了同样的错误

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.我尝试了各种方法,例如使用beginBackgroundTaskWithExpirationHandler请求更多时间执行(不起作用,因为导出在时间用完之前被中断),在应用程序功能中添加音频作为后台模式(不起作用,可能是因为我使用 AVMutableVideoComposition?),在主线程外的自定义队列中开始导出,但没有任何效果。

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.我试图听UIApplicationDidEnterBackgroundNotification并在应用程序已经在后台后重新启动导出,但我得到了同样的错误。

The discussion in this thread hints that it could be because AVMutableVideoComposition can not be used in the background:该线程中的讨论暗示可能是因为AVMutableVideoComposition无法在后台使用:

Starting AVAssetExportSession in the Background 在后台启动 AVAssetExportSession

Is this still true for iOS 8?对于 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.非常感谢任何帮助,我在这个问题上花了很多时间。

iOS 13 tested. iOS 13 已测试。 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.需要后台模式。

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.不幸的是,iOS 8 仍然如此。当应用程序在后台时,您无法导出,但可以在应用程序进入前台时立即开始导出。 Just save the AVMutableCompostion before your app goes to background.只需在您的应用程序进入后台之前保存AVMutableCompostion This can be done in applicationDidEnterBackground .这可以在applicationDidEnterBackground完成。 Then you can start export again in applicationDidBecomeActive .然后您可以在applicationDidBecomeActive再次开始导出。

Note : You have to start the export all over again.注意:您必须重新开始导出。 It can not be resumed if not finished before entering background.如果在进入后台之前未完成,则无法恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM