2013-12-16 28 views
4

我一直在尋找的答案2天的時候保存文件,但我似乎無法得到它的權利......停止攝像機記錄並輸入背景

我有一個示例應用程序,它記錄的音頻和視頻從設備使用AVCaptureSession和AVCaptureMovieFileOutput。

當我開始記錄我打電話:

[self.movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 

並開始記錄到文件中。如果我再次按下按鈕,停止記錄

[self.movieFileOutput stopRecording]; 

一切運作良好,但是當我進入後臺(來電或首頁新聞)我得到的委託方法的錯誤:didFinishRecordingToOutputFileAtURL

我想要的動作應在進入後臺時保存/完成文件。如果我調用stopDcording on「applicationDidEnterBackground」它將在調用applicationDidEnterBackground之前進入後臺。進入活動狀態時,它被稱爲....並生成一個錯誤,並保留一個損壞的電影文件...

它似乎沒有足夠的時間來保存該文件。

我在這裏錯過了什麼?

這是我的錯誤

Error Domain=AVFoundationErrorDomain Code=-11818 "Recording Stopped" UserInfo=0x17594e20 {NSLocalizedRecoverySuggestion=Stop any other actions using the recording device and try again., NSUnderlyingError=0x175d3500 "The operation couldn’t be completed. (OSStatus error -16133.)", NSLocalizedDescription=Recording Stopped}

AVErrorSessionWasInterrupted = -11818 
+0

我也試過蘋果AVDemo應用程序,它也給出了相同的錯誤。似乎無論如何都會拋出錯誤。不同的是,在蘋果的代碼中,他們有時間使用UIBackgroundTaskIdentifier – Michiel

回答

3

NSOperationQueue是執行多線程任務,以避免阻塞主線程的推薦方式。後臺線程用於您希望在應用程序處於非活動狀態時執行的任務,如GPS指示或音頻流。

如果您的應用程序在前臺運行,則根本不需要後臺線程。

對於簡單的任務,你可以在操作使用塊添加到隊列:

NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init]; 
[operationQueue addOperationWithBlock:^{ 
    // Perform long-running tasks without blocking main thread 
}]; 

更多信息有關NSOperationQueuehow to use it

- (void)applicationWillResignActive:(UIApplication *)application { 
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 

     // Wait until the pending operations finish 
     [operationQueue waitUntilAllOperationsAreFinished]; 

     [application endBackgroundTask: bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }]; 
} 
+0

執行大量代碼。這是我走的路。我沒有從蘋果的示例代碼中實現這段代碼,但是我犯了一個愚蠢的錯誤...每次電影被保存並因此看起來已損壞-1,我的文件擴展名被更改...... – Michiel

1

您可以處理在applicationWillResignActive:保存,然後您可以繼續在後臺進程。

+0

這不起作用,不知何故,我必須確保會話在進入後臺時不會中斷。 – Michiel

+0

然後你必須做另一個線程..你必須處理該線程,當它進入後臺.. –

+0

檢查,applicationWillResignActive並沒有給我足夠的時間進行處理。在MelihBüyükbayram它呢! – Michiel