public void FinishedRecording (AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject [] connections, NSError error)
{
// Note that currentBackgroundRecordingID is used to end the background task associated with this recording.
// This allows a new recording to be started, associated with a new UIBackgroundTaskIdentifier, once the movie file output's Recording property
// is back to false — which happens sometime after this method returns.
// Note: Since we use a unique file path for each recording, a new recording will not overwrite a recording currently being saved.
var currentBackgroundRecordingID = backgroundRecordingID;
backgroundRecordingID = -1;
Action cleanup = () => {
NSError err;
if (NSFileManager.DefaultManager.FileExists (outputFileUrl.Path))
NSFileManager.DefaultManager.Remove (outputFileUrl, out err);
if (currentBackgroundRecordingID != -1)
UIApplication.SharedApplication.EndBackgroundTask (currentBackgroundRecordingID);
};
var success = true;
if (error != null) {
Console.WriteLine ($"Error occurred while capturing movie: {error}");
success = error.UserInfo [AVErrorKeys.RecordingSuccessfullyFinished].AsBool ();
}
if (success) {
PHPhotoLibrary.RequestAuthorization (status => {
if (status == PHAuthorizationStatus.Authorized) {
// Save the movie file to the photo library and cleanup
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges (() => {
// In iOS 9 and later, it's possible to move the file into the photo library without duplicating the file data.
// This avoids using double the disk space during save, which can make a difference on devices with limited free disk space.
var options = new PHAssetResourceCreationOptions { ShouldMoveFile = true };
PHAssetCreationRequest changeRequest = PHAssetCreationRequest.CreationRequestForAsset ();
changeRequest.AddResource (PHAssetResourceType.Video, outputFileUrl, options);
}, (result, err) => {
if (!result)
Console.WriteLine ($"Could not save movie to photo library: {err}");
cleanup ();
});
} else {
cleanup ();
}
});
} else {
cleanup ();
}
// Enable the Camera and Record buttons to let the user switch camera and start another recording
DispatchQueue.MainQueue.DispatchAsync (() => {
// Only enable the ability to change camera if the device has more than one camera
CameraButton.Enabled = (AVCaptureDevice.DevicesWithMediaType (AVMediaType.Video).Length > 1);
RecordButton.Enabled = CaptureModeControl.SelectedSegment == (int)CaptureMode.Movie;
RecordButton.SetTitle ("Record", UIControlState.Normal);
CaptureModeControl.Enabled = true;
});
}