c# 實現錄屏

錄屏,錄屏資源銷燬要放在線程中執行

  public class ScreenVideoRecorder
    {

        private Recorder recorder;

        public ScreenVideoRecorder()
        {

        }

        /// <summary>
        /// 錄屏
        /// </summary>
        /// <param name="recordPath"></param>
        /// <param name="taskid"></param>
        /// <param name="qualityLevel">0low 1middle 2high</param>
        public void CreateRecording(string recordPath, string taskid, int qualityLevel = 0)
        {
            if (recorder != null)
            {
                EndRecording();
                return;
            }
            RecorderOptions options = new RecorderOptions
            {
                VideoEncoderOptions = new VideoEncoderOptions
                {
                    Bitrate = 8000 * 1000,
                    Framerate = 10,
                    IsFixedFramerate = true,
                    Quality = 30,
                    IsLowLatencyEnabled = true
                },
            };
            switch (qualityLevel)
            {
                case 0:
                    options = new RecorderOptions
                    {
                        VideoEncoderOptions = new VideoEncoderOptions
                        {
                            Bitrate = 8000 * 1000,
                            Framerate = 20,
                            IsFixedFramerate = true,
                            Quality = 30,
                            IsLowLatencyEnabled = true
                        },
                    };
                    break;
                case 1:
                    options = new RecorderOptions
                    {
                        VideoEncoderOptions = new VideoEncoderOptions
                        {
                            Bitrate = 8000 * 1000,
                            Framerate = 30,
                            IsFixedFramerate = true,
                            Quality = 50,
                            IsLowLatencyEnabled = true
                        },
                    };
                    break;
                case 2:
                    options = new RecorderOptions
                    {
                        VideoEncoderOptions = new VideoEncoderOptions
                        {
                            Bitrate = 8000 * 1000,
                            Framerate = 50,
                            IsFixedFramerate = true,
                            Quality = 70,
                            IsLowLatencyEnabled = true
                        },
                    };
                    break;
            }

            string timestamp = DateTime.Now.ToString(" yyyy-MM-dd HH-mm-ss");
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string videoPath = Path.Combine(recordPath, date, taskid + timestamp + ".mp4");
            if (recorder == null)
            {
                recorder = Recorder.CreateRecorder(options);
                recorder.OnRecordingComplete += Rec_OnRecordingComplete;
                recorder.OnRecordingFailed += Rec_OnRecordingFailed;
                recorder.OnStatusChanged += Rec_OnStatusChanged;
                //Record to a file
                recorder.Record(videoPath);
            }
        }

        public void EndRecording()
        {
            recorder?.Stop();
        }

        private void Rec_OnRecordingComplete(object sender, RecordingCompleteEventArgs e)
        {
            //Get the file path if recorded to a file
            string path = e.FilePath;
            Task.Run(() =>
            {
                recorder?.Dispose();
                recorder = null;
            });
           
        }

        private void Rec_OnRecordingFailed(object sender, RecordingFailedEventArgs e)
        {
            string error = e.Error;
            Task.Run(() =>
            {
                recorder?.Dispose();
                recorder = null;
            });
        }

        private void Rec_OnStatusChanged(object sender, RecordingStatusEventArgs e)
        {
            RecorderStatus status = e.Status;
        }

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章