Ejemplo n.º 1
0
        static void Main()
        {
            var fileName = Path.Combine(Environment.CurrentDirectory, "recording.mp4");

            {
                using var stream   = File.Create(fileName);
                using var recorder = ScreenRecorder.Create(stream);

                Console.WriteLine($"Recording in {fileName}.");
                Console.Write("Press any key to stop.");
                Console.Read();
            }

            Console.Write("Recording finished.");
        }
        public static ScreenRecorder Create(Stream output, ScreenRecorderOptions?options = null)
        {
            options ??= ScreenRecorderOptions.Default;
            var device = Direct3D11Helpers.CreateDevice();

            try
            {
                var recorder         = new ScreenRecorder(device, options);
                var prepareTranscode = recorder.Transcoder
                                       .PrepareMediaStreamSourceTranscodeAsync(recorder.Source, output.AsRandomAccessStream(), recorder.Profile);

                if (prepareTranscode.Status == AsyncStatus.Started)
                {
                    using var waitHandle       = new ManualResetEvent(false);
                    prepareTranscode.Completed = (_, __) => waitHandle.Set();
                    waitHandle.WaitOne();
                }

                prepareTranscode.GetResults().TranscodeAsync().Completed = (progress, status) =>
                {
                    try
                    {
                        progress.GetResults();
                    }
                    catch (Exception ex)
                    {
                        recorder.m_exception = ExceptionDispatchInfo.Capture(ex);
                    }
                    finally
                    {
                        recorder.m_stopped.Set();
                    }
                };

                return(recorder);
            }
            catch
            {
                device.Dispose();
                throw;
            }
        }