void ToggleMovieRecording (UIButton recordButton)
{
var output = MovieFileOutput;
if (output == null)
return;
// Disable the Camera button until recording finishes, and disable
// the Record button until recording starts or finishes.
// See the AVCaptureFileOutputRecordingDelegate methods.
CameraButton.Enabled = false;
recordButton.Enabled = false;
CaptureModeControl.Enabled = false;
// Retrieve the video preview layer's video orientation on the main queue
// before entering the session queue.We do this to ensure UI elements are
// accessed on the main thread and session configuration is done on the session queue.
var videoPreviewLayerOrientation = PreviewView.VideoPreviewLayer.Connection.VideoOrientation;
sessionQueue.DispatchAsync (() => {
if (!output.Recording) {
if (UIDevice.CurrentDevice.IsMultitaskingSupported) {
// Setup background task. This is needed because the IAVCaptureFileOutputRecordingDelegate.FinishedRecording
// callback is not received until AVCam returns to the foreground unless you request background execution time.
// This also ensures that there will be time to write the file to the photo library when AVCam is backgrounded.
// To conclude this background execution, UIApplication.SharedApplication.EndBackgroundTask is called in
// IAVCaptureFileOutputRecordingDelegate.FinishedRecording after the recorded file has been saved.
backgroundRecordingID = UIApplication.SharedApplication.BeginBackgroundTask (null);
}
// Update the orientation on the movie file output video connection before starting recording.
AVCaptureConnection connection = MovieFileOutput?.ConnectionFromMediaType (AVMediaType.Video);
if (connection != null)
connection.VideoOrientation = videoPreviewLayerOrientation;
// Start recording to a temporary file.
var outputFileName = new NSUuid ().AsString ();
var outputFilePath = Path.Combine (Path.GetTempPath (), Path.ChangeExtension (outputFileName, "mov"));
output.StartRecordingToOutputFile (NSUrl.FromFilename (outputFilePath), this);
} else {
output.StopRecording ();
}
});
}