Inheritance: ViewModelBase
示例#1
0
        // Call before Exit to free Resources
        public void Dispose()
        {
            HotKeyManager.Dispose();
            SystemTrayManager.Dispose();

            AudioViewModel.Dispose();

            Settings.Save();
        }
        public MainViewModel()
        {
            _timer          = new Timer(1000);
            _timer.Elapsed += TimerOnElapsed;

            #region Commands
            ScreenShotCommand = new DelegateCommand(CaptureScreenShot, () => _canScreenShot);

            RecordCommand = new DelegateCommand(() =>
            {
                if (ReadyToRecord)
                {
                    StartRecording();
                }
                else
                {
                    StopRecording();
                }
            }, () => _canRecord);

            RefreshCommand = new DelegateCommand(() =>
            {
                VideoViewModel.RefreshVideoSources();

                VideoViewModel.RefreshCodecs();

                AudioViewModel.RefreshAudioSources();

                Status = $"{VideoViewModel.AvailableCodecs.Count} Encoder(s) and " +
                         $"{AudioViewModel.AvailableRecordingSources.Count + AudioViewModel.AvailableLoopbackSources.Count - 2} AudioDevice(s) found";
            });

            OpenOutputFolderCommand = new DelegateCommand(() => Process.Start("explorer.exe", OutPath));

            PauseCommand = new DelegateCommand(() =>
            {
                if (IsPaused)
                {
                    _recorder.Start();
                    _timer.Start();

                    IsPaused = false;
                    Status   = "Recording...";
                }
                else
                {
                    _recorder.Pause();
                    _timer.Stop();

                    IsPaused = true;
                    Status   = "Paused";
                }
            }, () => !ReadyToRecord && _recorder != null);

            SelectOutputFolderCommand = new DelegateCommand(() =>
            {
                var dlg = new FolderBrowserDialog
                {
                    SelectedPath = OutPath,
                    Description  = "Select Output Folder"
                };

                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                OutPath = dlg.SelectedPath;
                Settings.Default.OutputPath = dlg.SelectedPath;
                Settings.Default.Save();
            });
            #endregion

            //Populate Available Codecs, Audio and Video Sources ComboBoxes
            RefreshCommand.Execute(null);

            AudioViewModel.PropertyChanged += (Sender, Args) =>
            {
                if (Args.PropertyName == nameof(AudioViewModel.SelectedRecordingSource) ||
                    Args.PropertyName == nameof(AudioViewModel.SelectedLoopbackSource))
                {
                    CheckFunctionalityAvailability();
                }
            };

            VideoViewModel.PropertyChanged += (Sender, Args) =>
            {
                if (Args.PropertyName == nameof(VideoViewModel.SelectedVideoSource))
                {
                    CheckFunctionalityAvailability();
                }
            };

            _cursor = new MouseCursor(OthersViewModel.Cursor);

            OthersViewModel.PropertyChanged += (Sender, Args) =>
            {
                switch (Args.PropertyName)
                {
                case nameof(OthersViewModel.RegionSelectorVisible):
                    VideoViewModel.RefreshVideoSources();
                    break;

                case nameof(OthersViewModel.Cursor):
                    _cursor.Include = OthersViewModel.Cursor;
                    break;
                }
            };

            // If Output Dircetory is not set. Set it to Documents\Captura\
            if (string.IsNullOrWhiteSpace(OutPath))
            {
                OutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Captura\\");
            }

            // Create the Output Directory if it does not exist
            if (!Directory.Exists(OutPath))
            {
                Directory.CreateDirectory(OutPath);
            }

            Settings.Default.OutputPath = OutPath;
            Settings.Default.Save();
        }
        void StartRecording()
        {
            var duration = OthersViewModel.Duration;
            var delay    = OthersViewModel.StartDelay;

            if (duration != 0 && (delay * 1000 > duration))
            {
                Status = "Delay cannot be greater than Duration";
                SystemSounds.Asterisk.Play();
                return;
            }

            if (OthersViewModel.MinimizeOnStart)
            {
                WindowState = WindowState.Minimized;
            }

            ReadyToRecord = false;

            var noVideo = VideoViewModel.SelectedVideoSourceKind == VideoSourceKind.NoVideo;

            var extension = noVideo
                            //? (AudioViewModel.Encode && AudioViewModel.SelectedAudioSource is WaveInDevice ? ".mp3" : ".wav")
                ? ".wav"
                : (VideoViewModel.SelectedCodec.Name == "Gif" ? ".gif" : ".avi");

            _currentFileName = Path.Combine(OutPath, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + extension);

            Status = delay > 0 ? $"Recording from t = {delay} ms..." : "Recording...";

            _timer.Stop();
            TimeSpan = TimeSpan.Zero;

            var audioSource = AudioViewModel.GetAudioSource();

            var imgProvider = GetImageProvider();

            var videoEncoder = GetVideoFileWriter(imgProvider);

            if (_recorder == null)
            {
                if (noVideo)
                {
                    _recorder = new AudioRecorder(audioSource, AudioViewModel.GetAudioFileWriter(_currentFileName, audioSource.WaveFormat));
                }

                else
                {
                    _recorder = new Recorder(videoEncoder, imgProvider, VideoViewModel.FrameRate, audioSource);
                }
            }

            _recorder.RecordingStopped += (s, E) =>
            {
                OnStopped();

                if (E?.Error == null)
                {
                    return;
                }

                Status = "Error";
                MessageBox.Show(E.ToString());
            };

            RecentViewModel.Add(_currentFileName, videoEncoder == null ? RecentItemType.Audio : RecentItemType.Video);

            _recorder.Start(delay);

            _timer.Start();
        }