错误:多次导出AsynchronouslyWithCompletionHandler。在 avfoundation 中恢复导出

发布于 2024-11-28 05:04:17 字数 1996 浏览 5 评论 0原文

您好,我正在开发一个 iPhone 应用程序,它创建一个 AVAssetExportSession 对象来保存和创建视频文件。一切正常(文件已创建并正确保存在文档文件夹中),除非我尝试恢复保存过程。如果我按下主页按钮退出应用程序(或者如果我收到电话或短信),exportAsynchronouslyWithCompletionHandler 方法将失败。苹果的文档称:

“如果您尝试覆盖现有文件,或在应用程序沙箱之外写入文件,导出将会失败。如果您需要覆盖现有文件,则必须先将其删除。如果出现以下情况,导出也可能会失败

:是来电

您的应用程序在后台运行,而另一个应用程序开始播放

在这些情况下,您通常应该通知用户导出失败,然后允许用户重新启动导出。”

所以我尝试取消导出,然后在用户重新打开应用程序时重新启动它。第二次调用exportAsynchronouslyWithCompletionHandler时出现此错误:

“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法调用exportAsynchronouslyWithCompletionHandler:多次。'”

似乎在[asset CancelExport]之后exportAsynchronouslyWithCompletionHandler仍然存在,它有可能吗?我还尝试将资产导出到另一个视图控制器以进行第二次调用,但结果是相同的。我如何在中断后重复保存而不丢失我的资产(它是由另一个视图控制器构建的,如果没有新的用户交互我无法重建它)?

这是我的代码:

-(void)save{

[asset exportAsynchronouslyWithCompletionHandler:^
 {
     switch (asset.status) 
     {
         case AVAssetExportSessionStatusCompleted: 
         {


             [self performSelectorOnMainThread:@selector(lanciosuccess) withObject:nil waitUntilDone:NO];

             break;
         }

         case AVAssetExportSessionStatusFailed:
         {

             [asset cancelExport];
             [self performSelectorOnMainThread:@selector(sospendi) withObject:nil waitUntilDone:NO];

             break;
         }
         case AVAssetExportSessionStatusUnknown: 
         {
              NSLog (@"unknow");



             break;
         }
         case AVAssetExportSessionStatusWaiting: 
         {
              NSLog (@"waiting");



             break;
         }
         case AVAssetExportSessionStatusCancelled: 
         {
              NSLog (@"cancelled");



             break;
         }


     };

 }];

}

-(void)sospendi{

if ([[NSFileManager defaultManager] fileExistsAtPath:[asset.outputURL path]]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:[asset.outputURL path] error:nil];
}

[self save];

}

Hi i'm developing an iphone application that creates an AVAssetExportSession object to save and create a video file. All is ok,(the file is created and correctly saved in the documents folder) except when i try to resume a saving process. If i push the home button to exit the app (or if i receive a call or a sms) the method exportAsynchronouslyWithCompletionHandler fail. Apple's documentation say:

"The export will fail if you try to overwrite an existing file, or write a file outside of the application’s sandbox. If you need to overwrite an existing file, you must remove it first. Export may also fail if:

There is an incoming phone call

Your application is in the background and another application starts playback

In these situations, you should typically inform the user that the export failed, then allow the user to restart the export."

So i try to cancel the exportation and then restart it when the user reopen the app. I have this error when exportAsynchronouslyWithCompletionHandler is called for the second time:

" Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call exportAsynchronouslyWithCompletionHandler: more than once.'"

It seems that after [asset CancelExport] exportAsynchronouslyWithCompletionHandler remains alive,it is possible? I have also tried to export the asset to another view controller for the second time calling but the result is the same. How i can do to repeat saving after an interruption without losing my asset (it's build by another view controller and i can't rebuild it without a new user interaction)?

This is my code:

-(void)save{

[asset exportAsynchronouslyWithCompletionHandler:^
 {
     switch (asset.status) 
     {
         case AVAssetExportSessionStatusCompleted: 
         {


             [self performSelectorOnMainThread:@selector(lanciosuccess) withObject:nil waitUntilDone:NO];

             break;
         }

         case AVAssetExportSessionStatusFailed:
         {

             [asset cancelExport];
             [self performSelectorOnMainThread:@selector(sospendi) withObject:nil waitUntilDone:NO];

             break;
         }
         case AVAssetExportSessionStatusUnknown: 
         {
              NSLog (@"unknow");



             break;
         }
         case AVAssetExportSessionStatusWaiting: 
         {
              NSLog (@"waiting");



             break;
         }
         case AVAssetExportSessionStatusCancelled: 
         {
              NSLog (@"cancelled");



             break;
         }


     };

 }];

}

-(void)sospendi{

if ([[NSFileManager defaultManager] fileExistsAtPath:[asset.outputURL path]]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:[asset.outputURL path] error:nil];
}

[self save];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文