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();
} }
} }
@ -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;