Pause AVFoundation application causing video file corruption

I am developing a camera app using AVCaptureMovieFileOutput. I record video with

[movieFileOutput_ startRecordingToOutputFileURL:url recordingDelegate:self];

and I pressed the home button or the power button to pause the app. To stop and save the video before pausing the application, I have implemented applicationWillResignActive like this.

NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self selector:@selector(applicationWillResignActive)
     name:UIApplicationWillResignActiveNotification object:NULL];

      


- (void)applicationWillResignActive{
    isApplicationActive_ = NO;
    if(self.isRecordingVideo == NO){
        return;
    }

    backgroundVideoSavingId_ = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundVideoSavingId_ != UIBackgroundTaskInvalid) {
                [[UIApplication sharedApplication] endBackgroundTask:backgroundVideoSavingId_];
                backgroundVideoSavingId_ = UIBackgroundTaskInvalid;
            }
        });
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        dispatch_async(dispatch_get_main_queue(), ^{
            [videoElapsedTimer_ invalidate];
            videoElapsedTimeLabel_.text = @"";
            [movieFileOutput_ stopRecording];
        });

        while(backgroundVideoSavingId_ != UIBackgroundTaskInvalid ||
              isApplicationActive_){
            [NSThread sleepForTimeInterval:1];
        }
        //[self playVideoBeepSound];
        backgroundRecordingId_ = UIBackgroundTaskInvalid;
        backgroundVideoSavingId_ = UIBackgroundTaskInvalid;
    });
}

      

And the conclusion of the output is as follows,

/*!
 * did finish recording
 */
- (void) captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)anOutputFileURL
                    fromConnections:(NSArray *)connections
                              error:(NSError *)error
{    
    if([self.delegate respondsToSelector:@selector(cameraController:didFinishRecordingVideoToOutputFileURL:error:)]){
        currentVideoURL_ = nil;
        [self.delegate cameraController:self didFinishRecordingVideoToOutputFileURL:anOutputFileURL error:error];
    }

    if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [[UIApplication sharedApplication] endBackgroundTask:backgroundVideoSavingId_];
        backgroundVideoSavingId_ = UIBackgroundTaskInvalid;
    }
}

      

And the problem is that often I get the error

-Error Domain=NSOSStatusErrorDomain Code=-12894 "The operation couldn't be completed. (OSStatus error -12894.)"

      

``,

I did a lot to figure out this problem and I couldn't ... What's wrong with my code ...?

Any help is appreciated. Thank.

+3


source to share





All Articles