2
2
using System . ComponentModel ;
3
3
using System . Linq ;
4
4
using System . Media ;
5
+ using System . Threading ;
5
6
using System . Threading . Tasks ;
6
7
using System . Windows ;
7
8
using System . Windows . Controls ;
@@ -61,8 +62,9 @@ public partial class MainWindow : IDisposable
61
62
private bool _isArrowKeyPressed = false ;
62
63
63
64
// Window Sound Effects
64
- private MediaPlayer animationSoundWMP ;
65
- private SoundPlayer animationSoundWPF ;
65
+ private MediaPlayer _animationSoundWMP ;
66
+ private SoundPlayer _animationSoundWPF ;
67
+ private readonly Lock _soundLock = new ( ) ;
66
68
67
69
// Window WndProc
68
70
private HwndSource _hwndSource ;
@@ -93,6 +95,7 @@ public MainWindow()
93
95
UpdatePosition ( ) ;
94
96
95
97
InitSoundEffects ( ) ;
98
+ RegisterSoundEffectsEvent ( ) ;
96
99
DataObject . AddPastingHandler ( QueryTextBox , QueryTextBox_OnPaste ) ;
97
100
_viewModel . ActualApplicationThemeChanged += ViewModel_ActualApplicationThemeChanged ;
98
101
}
@@ -666,16 +669,6 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
666
669
handled = true ;
667
670
}
668
671
break ;
669
- case Win32Helper . WM_POWERBROADCAST : // Handle power broadcast messages
670
- // https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast
671
- if ( wParam . ToInt32 ( ) == Win32Helper . PBT_APMRESUMEAUTOMATIC )
672
- {
673
- // Fix for sound not playing after sleep / hibernate
674
- // https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps
675
- InitSoundEffects ( ) ;
676
- }
677
- handled = true ;
678
- break ;
679
672
}
680
673
681
674
return IntPtr . Zero ;
@@ -687,31 +680,78 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
687
680
688
681
private void InitSoundEffects ( )
689
682
{
690
- if ( _settings . WMPInstalled )
683
+ lock ( _soundLock )
691
684
{
692
- animationSoundWMP ? . Close ( ) ;
693
- animationSoundWMP = new MediaPlayer ( ) ;
694
- animationSoundWMP . Open ( new Uri ( AppContext . BaseDirectory + "Resources\\ open.wav" ) ) ;
685
+ if ( _settings . WMPInstalled )
686
+ {
687
+ _animationSoundWMP ? . Close ( ) ;
688
+ _animationSoundWMP = new MediaPlayer ( ) ;
689
+ _animationSoundWMP . Open ( new Uri ( AppContext . BaseDirectory + "Resources\\ open.wav" ) ) ;
690
+ }
691
+ else
692
+ {
693
+ _animationSoundWPF ? . Dispose ( ) ;
694
+ _animationSoundWPF = new SoundPlayer ( AppContext . BaseDirectory + "Resources\\ open.wav" ) ;
695
+ _animationSoundWPF . Load ( ) ;
696
+ }
695
697
}
696
- else
698
+ }
699
+
700
+ private void SoundPlay ( )
701
+ {
702
+ lock ( _soundLock )
697
703
{
698
- animationSoundWPF ? . Dispose ( ) ;
699
- animationSoundWPF = new SoundPlayer ( AppContext . BaseDirectory + "Resources\\ open.wav" ) ;
700
- animationSoundWPF . Load ( ) ;
704
+ if ( _settings . WMPInstalled )
705
+ {
706
+ _animationSoundWMP . Position = TimeSpan . Zero ;
707
+ _animationSoundWMP . Volume = _settings . SoundVolume / 100.0 ;
708
+ _animationSoundWMP . Play ( ) ;
709
+ }
710
+ else
711
+ {
712
+ _animationSoundWPF . Play ( ) ;
713
+ }
701
714
}
702
715
}
703
716
704
- private void SoundPlay ( )
717
+ private void RegisterSoundEffectsEvent ( )
705
718
{
706
- if ( _settings . WMPInstalled )
719
+ // Fix for sound not playing after sleep / hibernate for both modern standby and legacy standby
720
+ // https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps
721
+ try
707
722
{
708
- animationSoundWMP . Position = TimeSpan . Zero ;
709
- animationSoundWMP . Volume = _settings . SoundVolume / 100.0 ;
710
- animationSoundWMP . Play ( ) ;
723
+ Win32Helper . RegisterSleepModeListener ( ( ) =>
724
+ {
725
+ if ( Application . Current == null )
726
+ {
727
+ return ;
728
+ }
729
+
730
+ // We must run InitSoundEffects on UI thread because MediaPlayer is a DispatcherObject
731
+ if ( ! Application . Current . Dispatcher . CheckAccess ( ) )
732
+ {
733
+ Application . Current . Dispatcher . Invoke ( InitSoundEffects ) ;
734
+ return ;
735
+ }
736
+
737
+ InitSoundEffects ( ) ;
738
+ } ) ;
711
739
}
712
- else
740
+ catch ( Exception e )
741
+ {
742
+ App . API . LogException ( ClassName , "Failed to register sound effect event" , e ) ;
743
+ }
744
+ }
745
+
746
+ private static void UnregisterSoundEffectsEvent ( )
747
+ {
748
+ try
749
+ {
750
+ Win32Helper . UnregisterSleepModeListener ( ) ;
751
+ }
752
+ catch ( Exception e )
713
753
{
714
- animationSoundWPF . Play ( ) ;
754
+ App . API . LogException ( ClassName , "Failed to unregister sound effect event" , e ) ;
715
755
}
716
756
}
717
757
@@ -1436,9 +1476,10 @@ protected virtual void Dispose(bool disposing)
1436
1476
{
1437
1477
_hwndSource ? . Dispose ( ) ;
1438
1478
_notifyIcon ? . Dispose ( ) ;
1439
- animationSoundWMP ? . Close ( ) ;
1440
- animationSoundWPF ? . Dispose ( ) ;
1479
+ _animationSoundWMP ? . Close ( ) ;
1480
+ _animationSoundWPF ? . Dispose ( ) ;
1441
1481
_viewModel . ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged ;
1482
+ UnregisterSoundEffectsEvent ( ) ;
1442
1483
}
1443
1484
1444
1485
_disposed = true ;
0 commit comments