暂停AVFoundation应用程序导致视频文件损坏

时间:2012-03-31 09:49:56

标签: ios5 avfoundation

我正在使用AVCaptureMovieFileOutput开发相机应用。 我正在用

录制视频

[movieFileOutput_ startRecordingToOutputFileURL:url recordingDelegate:self];

然后按下主页按钮或电源按钮暂停应用程序。 要在暂停应用程序之前停止并保存视频,我已经像这样实现了applicationWillResignActive。

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;
    });
}

捕获输出就是这样,

/*!
 * 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;
    }
}

问题是,我常常得到错误

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

```

我做了很多事来弄清楚这个问题,我不能...... 我的代码有什么问题......?

任何帮助表示赞赏。感谢。

0 个答案:

没有答案