按Home键停止视频录制

时间:2012-10-25 10:22:29

标签: ios avfoundation

当按下主页按钮时,如何在AVFramework上安全地停止视频录制?

我希望我的应用程序像本机相机应用程序一样工作:在录制过程中按主页按钮会停止录制过程,然后进入后台模式。

在我的应用代表上,我致电[[videoController captureManager] stopRecording];,但在recordingDidFinishToOutputFileURL我收到错误消息:

  

使用录制设备停止任何其他操作,然后重试。

2 个答案:

答案 0 :(得分:2)

我建议您拨打电话,停止在您的应用代表的applicationWillResignActive:方法中录制视频。当您的应用从活动状态变为非活动状态时会触发此方法,根据UIApplicationDelegate docs,该应用会发生:

  

...对于某些类型的临时中断(例如来电或短信),或者当用户退出应用程序并开始转换到后台状态时。

代码看起来像这样:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[videoController captureManager] stopRecording];

    // Do anything else before app becomes inactive
}

答案 1 :(得分:2)

尝试恢复录制的视频...

    - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
    {
        BOOL recordedSuccessfully = NO;
        if ([error code] != noErr)
        {
            if(outputFileURL)
            {
                NSFileManager* manager = [NSFileManager defaultManager];
                NSString* path = outputFileURL.path;
                if([manager fileExistsAtPath:path])
                {
                    NSError *attributesError = nil;
                    NSDictionary *fileAttributes = [manager attributesOfItemAtPath:path error:&attributesError];
                    NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
                    long long fileSize = [fileSizeNumber longLongValue];
                    if(fileSize > 0)
                    {
                        NSLog(@"Recording finished with error, but trying to save whatever recorded");
                        // save whatever we have to camera roll.
                    }
                }
            }
        }
    }