async void OnRecordExecute() { if (RecorderState.Value == Models.RecorderState.NotRecording) { _systemTray.HideNotification(); if (_recordingModel.StartRecording()) { if (Settings.Tray.MinToTrayOnCaptureStart) { _mainWindow.IsVisible = false; } _audioPlayer.Play(SoundKind.Start); } else { _audioPlayer.Play(SoundKind.Error); } } else { _audioPlayer.Play(SoundKind.Stop); await _recordingModel.StopRecording(); } }
async Task StopRecording() { _audioPlayer.Play(SoundKind.Stop); FileRecentItem savingRecentItem = null; FileSaveNotification notification = null; var fileName = _recordingModel.CurrentFileName; var isVideo = _recordingModel.IsVideo; IVideoConverter postWriter = null; // Assume saving to file only when extension is present if (!_timerModel.Waiting && !string.IsNullOrWhiteSpace(_videoWritersViewModel.SelectedVideoWriter.Extension)) { savingRecentItem = new FileRecentItem(fileName, isVideo ? RecentFileType.Video : RecentFileType.Audio, true); _recentList.Add(savingRecentItem); notification = new FileSaveNotification(savingRecentItem); notification.OnDelete += () => savingRecentItem.RemoveCommand.ExecuteIfCan(); _systemTray.ShowNotification(notification); if (isVideo && Settings.Video.PostConvert) { postWriter = _videoWritersViewModel.SelectedPostWriter; } } var task = _recordingModel.StopRecording(); lock (_stopRecTaskLock) { _stopRecTasks.Add(task); } var wasWaiting = _timerModel.Waiting; _timerModel.Waiting = false; try { // Ensure saved await task; if (postWriter != null) { notification.Converting(); var progress = new Progress <int>(); progress.ProgressChanged += (S, E) => notification.Progress = E; var outFileName = Path.Combine( Path.GetDirectoryName(fileName), $"{Path.GetFileNameWithoutExtension(fileName)}.converted{postWriter.Extension}"); try { await postWriter.StartAsync(new VideoConverterArgs { AudioQuality = Settings.Audio.Quality, VideoQuality = Settings.Video.Quality, InputFile = fileName, FileName = outFileName }, progress); File.Delete(fileName); var targetFileName = Path.Combine( Path.GetDirectoryName(fileName), $"{Path.GetFileNameWithoutExtension(fileName)}{postWriter.Extension}"); File.Move(outFileName, targetFileName); savingRecentItem.Converted(targetFileName); notification.Converted(targetFileName); } catch (FFmpegNotFoundException e) { try { _ffmpegViewsProvider.ShowUnavailable(); } catch { // Show simpler message for cases the above fails _messageProvider.ShowException(e, e.Message); } } catch (Exception e) { _messageProvider.ShowException(e, "Conversion Failed"); } } lock (_stopRecTaskLock) { _stopRecTasks.Remove(task); } } catch (Exception e) { _messageProvider.ShowException(e, "Error occurred when stopping recording.\nThis might sometimes occur if you stop recording just as soon as you start it."); return; } if (wasWaiting) { try { File.Delete(fileName); } catch { // Ignore Errors } } if (savingRecentItem != null) { AfterSave(savingRecentItem, notification); } }
async Task StopRecording() { _audioPlayer.Play(SoundKind.Stop); FileRecentItem savingRecentItem = null; FileSaveNotification notification = null; var fileName = _recordingModel.CurrentFileName; var isVideo = _recordingModel.IsVideo; // Assume saving to file only when extension is present if (!_timerModel.Waiting && !string.IsNullOrWhiteSpace(_videoWritersViewModel.SelectedVideoWriter.Extension)) { savingRecentItem = new FileRecentItem(fileName, isVideo ? RecentFileType.Video : RecentFileType.Audio, true); _recentList.Add(savingRecentItem); notification = new FileSaveNotification(savingRecentItem); notification.OnDelete += () => savingRecentItem.RemoveCommand.ExecuteIfCan(); _systemTray.ShowNotification(notification); } var task = _recordingModel.StopRecording(); lock (_stopRecTaskLock) { _stopRecTasks.Add(task); } var wasWaiting = _timerModel.Waiting; _timerModel.Waiting = false; try { // Ensure saved await task; lock (_stopRecTaskLock) { _stopRecTasks.Remove(task); } } catch (Exception e) { _messageProvider.ShowException(e, "Error occurred when stopping recording.\nThis might sometimes occur if you stop recording just as soon as you start it."); return; } if (wasWaiting) { try { File.Delete(fileName); } catch { // Ignore Errors } } if (savingRecentItem != null) { AfterSave(savingRecentItem, notification); } }