Completion handler is not called on iOS7 GM

2020-02-25 07:51发布

I'm using AVAssetWriter, and it is perfectly working on iOS6.

The problem is, when I called finishWritingWithCompletionHandler, the completion handler is not called on iOS7 GM.

I called markAsFinished, and even endSessionAtSourceTime before I call finishWritingWithCompletionHandler.

It works fine on iOS6.

And even more, on iOS7, it works some times, and then it doesn't work again.

I don't know why, but it works if I call the method using alert view. So I tried performSelectorOnMainThread and inBackground, but it didn't help.

Any ideas?

5条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-25 08:00

This happen on ARC as well.

The simplest solution is to define properties of AVAssetWriter(and AVAssetReader I assume)

@property(nonatomic,strong) AVAssetWriter *assetWriter;
@property(nonatomic,strong) AVAssetReader *assetReader;

and then

self.assetWriter = [AVAssetWriter assetWriterWithURL:destURL
                                                       fileType:AVFileTypeWAVE
                                                          error:&assetError];

and in the completion block

              [assetWriterInput markAsFinished];
             [assetWriter finishWritingWithCompletionHandler:^{
                 [assetReader cancelReading];
                 completionBlock(self);

             }];
查看更多
【Aperson】
3楼-- · 2020-02-25 08:02

It also can happen if destination directory does not exist. In this case writing works fine, but no file created and the block is not called.

查看更多
可以哭但决不认输i
4楼-- · 2020-02-25 08:11

Ray Fix, you are right. We need to retain assetWriter. The easiest way is to use it inside the finishWritingWithCompletionHandler block:

        NSError *error = nil;

        AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path]
                                                                   fileType:AVFileType3GPP
                                                                      error:&error];
        //startWriting, session etc.  

        [videoWriter finishWritingWithCompletionHandler:^{
                NSLog(@"%@",videoWriter);
                NSLog(@"Write Ended");
            }];
查看更多
家丑人穷心不美
5楼-- · 2020-02-25 08:16

Apparently you need to retain the assetWriter now.

You might try retaining with a strong property it and see if your completion handler gets called. (Be sure to nil that property in completion handler.)

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-25 08:16

Retaining the assetwriter is very important but I also ran into a very strange intermittent failure even though my assetwriters were all retained (and never reused). The problem also wasn't a file name collision or missing directory because my file names are all based on CACurrentMediaTime() and don't change directories.

It seems if you don't set endSessionAtSourceTime: for the assetwriter every time, there's a very rare (but reproducible) chance the completion handler for finishWritingWithCompletionHandler: won't ever be called. If you wait a few seconds and check on the status of the assetwriter, it will be in the AVAssetWriterStatusFailure state and the error will be a non-descript "An unknown error occurred (-12763)". Additionally, changing the file format for the assetwriter doesn't see to have an effect on this problem. Lastly, this problem is probably only an issue if you need to rapidly record movies over and over again (as the chance for failure is probably 1/15 - 1/20).

So, just be sure to store the presentation time stamp for the last sample you're passing to the assetwriter and to call endSessionAtSourceTime: with that sample time right before you're about to call finishWritingWithCompletionHandler:.

查看更多
登录 后发表回答