Exemplo n.º 1
0
        void ToggleMovieRecording(NSObject sender)
        {
            /*
             *      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 videoPreviewLayerVideoOrientation = VideoPreviewLayer.Connection.VideoOrientation;

            sessionQueue.DispatchAsync(() =>
            {
                if (!movieFileOutput.Recording)
                {
                    if (UIDevice.CurrentDevice.IsMultitaskingSupported)
                    {
                        /*
                         *      Setup background task.
                         *      This is needed because the -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:]
                         *      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, -[endBackgroundTask:] is called in
                         *      -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:] 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.
                    var movieFileOutputConnection = movieFileOutput.ConnectionFromMediaType(AVMediaType.Video);
                    movieFileOutputConnection.VideoOrientation = videoPreviewLayerVideoOrientation;

                    // Use HEVC codec if supported
                    if (movieFileOutput.AvailableVideoCodecTypes.Where(codec => codec == AVVideo2.CodecHEVC).Any())
                    {
                        movieFileOutput.SetOutputSettings(new NSDictionary(AVVideo.CodecKey, AVVideo2.CodecHEVC), movieFileOutputConnection);
                    }

                    // Start recording to a temporary file.
                    var outputFileName         = Guid.NewGuid().ToString();
                    var livePhotoMovieFilePath = NSFileManager.DefaultManager.GetTemporaryDirectory().Append($"{outputFileName}.mov", false);
                    movieFileOutput.StartRecordingToOutputFile(livePhotoMovieFilePath, this);
                }
                else
                {
                    movieFileOutput.StopRecording();
                }
            });
        }