Fix sound effect issue after sleep or hiberation

This commit is contained in:
Jack251970 2025-05-11 20:56:21 +08:00
parent 63aabd5f1f
commit 71b6cb2ca5

View file

@ -22,6 +22,7 @@ using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
using Microsoft.Win32;
using ModernWpf.Controls; using ModernWpf.Controls;
using DataObject = System.Windows.DataObject; using DataObject = System.Windows.DataObject;
using Key = System.Windows.Input.Key; using Key = System.Windows.Input.Key;
@ -88,6 +89,8 @@ namespace Flow.Launcher
InitSoundEffects(); InitSoundEffects();
DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste); DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste);
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
} }
#endregion #endregion
@ -540,16 +543,29 @@ namespace Flow.Launcher
#region Window Sound Effects #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() private void InitSoundEffects()
{ {
if (_settings.WMPInstalled) if (_settings.WMPInstalled)
{ {
animationSoundWMP?.Close();
animationSoundWMP = new MediaPlayer(); animationSoundWMP = new MediaPlayer();
animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav")); animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav"));
} }
else else
{ {
animationSoundWPF?.Dispose();
animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
animationSoundWPF.Load();
} }
} }
@ -816,7 +832,7 @@ namespace Flow.Launcher
{ {
Name = progressBarAnimationName, Storyboard = progressBarStoryBoard Name = progressBarAnimationName, Storyboard = progressBarStoryBoard
}; };
var stopStoryboard = new StopStoryboard() var stopStoryboard = new StopStoryboard()
{ {
BeginStoryboardName = progressBarAnimationName BeginStoryboardName = progressBarAnimationName
@ -837,7 +853,7 @@ namespace Flow.Launcher
progressStyle.Triggers.Add(trigger); progressStyle.Triggers.Add(trigger);
ProgressBar.Style = progressStyle; ProgressBar.Style = progressStyle;
_viewModel.ProgressBarVisibility = Visibility.Hidden; _viewModel.ProgressBarVisibility = Visibility.Hidden;
} }
@ -885,7 +901,7 @@ namespace Flow.Launcher
Duration = TimeSpan.FromMilliseconds(animationLength), Duration = TimeSpan.FromMilliseconds(animationLength),
FillBehavior = FillBehavior.HoldEnd FillBehavior = FillBehavior.HoldEnd
}; };
var rightMargin = GetThicknessFromStyle(ClockPanel.Style, new Thickness(0, 0, DefaultRightMargin, 0)).Right; var rightMargin = GetThicknessFromStyle(ClockPanel.Style, new Thickness(0, 0, DefaultRightMargin, 0)).Right;
var thicknessAnimation = new ThicknessAnimation var thicknessAnimation = new ThicknessAnimation
@ -913,10 +929,10 @@ namespace Flow.Launcher
clocksb.Children.Add(ClockOpacity); clocksb.Children.Add(ClockOpacity);
iconsb.Children.Add(IconMotion); iconsb.Children.Add(IconMotion);
iconsb.Children.Add(IconOpacity); iconsb.Children.Add(IconOpacity);
_settings.WindowLeft = Left; _settings.WindowLeft = Left;
_isArrowKeyPressed = false; _isArrowKeyPressed = false;
clocksb.Begin(ClockPanel); clocksb.Begin(ClockPanel);
iconsb.Begin(SearchIcon); iconsb.Begin(SearchIcon);
} }
@ -1088,7 +1104,7 @@ namespace Flow.Launcher
{ {
e.Handled = true; e.Handled = true;
} }
#endregion #endregion
#region Placeholder #region Placeholder
@ -1140,7 +1156,7 @@ namespace Flow.Launcher
} }
#endregion #endregion
#region Search Delay #region Search Delay
private void QueryTextBox_TextChanged1(object sender, TextChangedEventArgs e) private void QueryTextBox_TextChanged1(object sender, TextChangedEventArgs e)
@ -1162,6 +1178,9 @@ namespace Flow.Launcher
{ {
_hwndSource?.Dispose(); _hwndSource?.Dispose();
_notifyIcon?.Dispose(); _notifyIcon?.Dispose();
animationSoundWMP?.Close();
animationSoundWPF?.Dispose();
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
} }
_disposed = true; _disposed = true;