简体   繁体   English

在后台运行 AVAssetExportSession

[英]Running AVAssetExportSession in the background

I cannot get AVAssetExportSession to work when the application is in the background.当应用程序在后台时,我无法让 AVAssetExportSession 工作。

My application has the Background Mode "Background Fetch" enabled.我的应用程序启用了后台模式“后台获取”。

When this UIApplicationDelegate method is called当这个 UIApplicationDelegate 方法被调用时

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)  

is called I use AVAssetExportSession to export an AVAsset, using the method:被称为我使用 AVAssetExportSession 导出一个 AVAsset,使用方法:

exportAsynchronouslyWithCompletionHandler exportAsynchronouslyWithCompletionHandler

I then receive this error:然后我收到此错误:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x147dae560 {Error Domain=NSOSStatusErrorDomain Code=-16980 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16980), NSLocalizedDescription=The operation could not be completed} 

However, when I have the background mode "Audio, Airplay and Picture in Picture", the export works.但是,当我使用背景模式“音频、Airplay 和画中画”时,导出工作。

This is great, however our submission is being rejected because we do not play any audio in the background.这很好,但是我们的提交被拒绝了,因为我们不在后台播放任何音频。

is there any other way to export video in the background?有没有其他方法可以在后台导出视频?

Cheers, Red干杯,红色

STEPS TO REPRODUCE I have created a sample project to show this issue.重现的步骤我创建了一个示例项目来展示这个问题。

Download from: http://up.red.to/WKo1MMstzD下载地址: http : //up.red.to/WKo1MMstzD

  • Run the app on a device在设备上运行应用程序
  • Accept the permissions接受权限
  • Press the home button按下主页按钮
  • In Xcode, go to Debug -> Simulate Background Refresh在 Xcode 中,转到 Debug -> Simulate Background Refresh
  • See error (printed in console and shown as a local notification)查看错误(在控制台中打印并显示为本地通知)

Your question:你的问题:

is there any other way to export video in the background?有没有其他方法可以在后台导出视频?

Absolutely, with some limitations.当然,有一些限制。

NSProcessInfo has a set of APIs for requesting more time to finish tasks when the application is in the background. NSProcessInfo有一组 API,用于在应用程序处于后台时请求更多时间来完成任务。 For example:例如:

id activity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityAutomaticTerminationDisabled reason:@"Good Reason"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    [[NSProcessInfo processInfo] endActivity:activity];
}];

That will get you more time.那会让你有更多的时间。 There are other APIs intended for synchronous operations, and in either case your application should still be prepared for the system to not allow more time.有用于同步操作其他的API,并且在任何情况下,你的应用程序仍然应该为系统不允许有更多的时间准备。 This was covered extensively in sessions at WWDC 2015.这在 WWDC 2015 的会议中得到了广泛的讨论。

You must also make sure that your background activity is not writing to a protected portion of the filesystem.您还必须确保您的后台活动没有写入文件系统的受保护部分。 Make sure the output location has the correct NSFileProtection attributes set to allow access even when the device is locked.确保输出位置设置了正确的NSFileProtection属性,即使设备被锁定也允许访问。

Now, specifically with background fetch you have a very limited amount of time to complete your work and call the background fetch completion handler.现在,特别是对于后台提取,您完成工作和调用后台提取完成处理程序的时间非常有限。 The background fetch API is intended to be used with NSURLSession to schedule background downloads that are executed out of your application process.后台获取 API 旨在与NSURLSession一起使用,以安排在应用程序进程之外执行的后台下载。 Using the NSProcessInfo API will probably not buy you any more time here, and the background fetch API is a very poor fit for encoding/exporting media.NSProcessInfo使用NSProcessInfo API 可能不会再为您NSProcessInfo更多时间,而且后台获取 API 非常不适合编码/导出媒体。 The time required to complete for even small media files would exceed the time limit for the background fetch handler.即使是小的媒体文件完成所需的时间也会超过后台提取处理程序的时间限制。

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

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