From 71b6cb2ca53ce28697fb9ed0ee75004aaf712e49 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 11 May 2025 20:56:21 +0800 Subject: [PATCH] Fix sound effect issue after sleep or hiberation --- Flow.Launcher/MainWindow.xaml.cs | 33 +++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index e243549e31e..46eeb2adc6e 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -22,6 +22,7 @@ using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.ViewModel; +using Microsoft.Win32; using ModernWpf.Controls; using DataObject = System.Windows.DataObject; using Key = System.Windows.Input.Key; @@ -88,6 +89,8 @@ public MainWindow() InitSoundEffects(); DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste); + + SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; } #endregion @@ -540,16 +543,29 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b #region Window Sound Effects + private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + // Fix for sound not playing after sleep / hibernate + // https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps + if (e.Mode == PowerModes.Resume) + { + InitSoundEffects(); + } + } + private void InitSoundEffects() { if (_settings.WMPInstalled) { + animationSoundWMP?.Close(); animationSoundWMP = new MediaPlayer(); animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav")); } else { + animationSoundWPF?.Dispose(); animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); + animationSoundWPF.Load(); } } @@ -816,7 +832,7 @@ private void InitProgressbarAnimation() { Name = progressBarAnimationName, Storyboard = progressBarStoryBoard }; - + var stopStoryboard = new StopStoryboard() { BeginStoryboardName = progressBarAnimationName @@ -837,7 +853,7 @@ private void InitProgressbarAnimation() progressStyle.Triggers.Add(trigger); ProgressBar.Style = progressStyle; - + _viewModel.ProgressBarVisibility = Visibility.Hidden; } @@ -885,7 +901,7 @@ private void WindowAnimation() Duration = TimeSpan.FromMilliseconds(animationLength), FillBehavior = FillBehavior.HoldEnd }; - + var rightMargin = GetThicknessFromStyle(ClockPanel.Style, new Thickness(0, 0, DefaultRightMargin, 0)).Right; var thicknessAnimation = new ThicknessAnimation @@ -913,10 +929,10 @@ private void WindowAnimation() clocksb.Children.Add(ClockOpacity); iconsb.Children.Add(IconMotion); iconsb.Children.Add(IconOpacity); - + _settings.WindowLeft = Left; _isArrowKeyPressed = false; - + clocksb.Begin(ClockPanel); iconsb.Begin(SearchIcon); } @@ -1088,7 +1104,7 @@ private void QueryTextBox_OnPreviewDragOver(object sender, DragEventArgs e) { e.Handled = true; } - + #endregion #region Placeholder @@ -1140,7 +1156,7 @@ private void SetupResizeMode() } #endregion - + #region Search Delay private void QueryTextBox_TextChanged1(object sender, TextChangedEventArgs e) @@ -1162,6 +1178,9 @@ protected virtual void Dispose(bool disposing) { _hwndSource?.Dispose(); _notifyIcon?.Dispose(); + animationSoundWMP?.Close(); + animationSoundWPF?.Dispose(); + SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; } _disposed = true;