Skip to content

Commit ea4360a

Browse files
authored
Merge pull request #3532 from Flow-Launcher/sound_effect_sleep_issue
Fix sound effect issue after sleep or hibernation
2 parents 63aabd5 + 71b6cb2 commit ea4360a

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Flow.Launcher.Infrastructure.UserSettings;
2323
using Flow.Launcher.Plugin.SharedCommands;
2424
using Flow.Launcher.ViewModel;
25+
using Microsoft.Win32;
2526
using ModernWpf.Controls;
2627
using DataObject = System.Windows.DataObject;
2728
using Key = System.Windows.Input.Key;
@@ -88,6 +89,8 @@ public MainWindow()
8889

8990
InitSoundEffects();
9091
DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste);
92+
93+
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
9194
}
9295

9396
#endregion
@@ -540,16 +543,29 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
540543

541544
#region Window Sound Effects
542545

546+
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
547+
{
548+
// Fix for sound not playing after sleep / hibernate
549+
// https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps
550+
if (e.Mode == PowerModes.Resume)
551+
{
552+
InitSoundEffects();
553+
}
554+
}
555+
543556
private void InitSoundEffects()
544557
{
545558
if (_settings.WMPInstalled)
546559
{
560+
animationSoundWMP?.Close();
547561
animationSoundWMP = new MediaPlayer();
548562
animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav"));
549563
}
550564
else
551565
{
566+
animationSoundWPF?.Dispose();
552567
animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
568+
animationSoundWPF.Load();
553569
}
554570
}
555571

@@ -816,7 +832,7 @@ private void InitProgressbarAnimation()
816832
{
817833
Name = progressBarAnimationName, Storyboard = progressBarStoryBoard
818834
};
819-
835+
820836
var stopStoryboard = new StopStoryboard()
821837
{
822838
BeginStoryboardName = progressBarAnimationName
@@ -837,7 +853,7 @@ private void InitProgressbarAnimation()
837853
progressStyle.Triggers.Add(trigger);
838854

839855
ProgressBar.Style = progressStyle;
840-
856+
841857
_viewModel.ProgressBarVisibility = Visibility.Hidden;
842858
}
843859

@@ -885,7 +901,7 @@ private void WindowAnimation()
885901
Duration = TimeSpan.FromMilliseconds(animationLength),
886902
FillBehavior = FillBehavior.HoldEnd
887903
};
888-
904+
889905
var rightMargin = GetThicknessFromStyle(ClockPanel.Style, new Thickness(0, 0, DefaultRightMargin, 0)).Right;
890906

891907
var thicknessAnimation = new ThicknessAnimation
@@ -913,10 +929,10 @@ private void WindowAnimation()
913929
clocksb.Children.Add(ClockOpacity);
914930
iconsb.Children.Add(IconMotion);
915931
iconsb.Children.Add(IconOpacity);
916-
932+
917933
_settings.WindowLeft = Left;
918934
_isArrowKeyPressed = false;
919-
935+
920936
clocksb.Begin(ClockPanel);
921937
iconsb.Begin(SearchIcon);
922938
}
@@ -1088,7 +1104,7 @@ private void QueryTextBox_OnPreviewDragOver(object sender, DragEventArgs e)
10881104
{
10891105
e.Handled = true;
10901106
}
1091-
1107+
10921108
#endregion
10931109

10941110
#region Placeholder
@@ -1140,7 +1156,7 @@ private void SetupResizeMode()
11401156
}
11411157

11421158
#endregion
1143-
1159+
11441160
#region Search Delay
11451161

11461162
private void QueryTextBox_TextChanged1(object sender, TextChangedEventArgs e)
@@ -1162,6 +1178,9 @@ protected virtual void Dispose(bool disposing)
11621178
{
11631179
_hwndSource?.Dispose();
11641180
_notifyIcon?.Dispose();
1181+
animationSoundWMP?.Close();
1182+
animationSoundWPF?.Dispose();
1183+
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
11651184
}
11661185

11671186
_disposed = true;

0 commit comments

Comments
 (0)