|
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; |
@@ -687,31 +689,37 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b |
687 | 689 |
|
688 | 690 | private void InitSoundEffects() |
689 | 691 | { |
690 | | - if (_settings.WMPInstalled) |
| 692 | + lock (_soundLock) |
691 | 693 | { |
692 | | - animationSoundWMP?.Close(); |
693 | | - animationSoundWMP = new MediaPlayer(); |
694 | | - animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav")); |
695 | | - } |
696 | | - else |
697 | | - { |
698 | | - animationSoundWPF?.Dispose(); |
699 | | - animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); |
700 | | - animationSoundWPF.Load(); |
| 694 | + if (_settings.WMPInstalled) |
| 695 | + { |
| 696 | + _animationSoundWMP?.Close(); |
| 697 | + _animationSoundWMP = new MediaPlayer(); |
| 698 | + _animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav")); |
| 699 | + } |
| 700 | + else |
| 701 | + { |
| 702 | + _animationSoundWPF?.Dispose(); |
| 703 | + _animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); |
| 704 | + _animationSoundWPF.Load(); |
| 705 | + } |
701 | 706 | } |
702 | 707 | } |
703 | 708 |
|
704 | 709 | private void SoundPlay() |
705 | 710 | { |
706 | | - if (_settings.WMPInstalled) |
| 711 | + lock (_soundLock) |
707 | 712 | { |
708 | | - animationSoundWMP.Position = TimeSpan.Zero; |
709 | | - animationSoundWMP.Volume = _settings.SoundVolume / 100.0; |
710 | | - animationSoundWMP.Play(); |
711 | | - } |
712 | | - else |
713 | | - { |
714 | | - animationSoundWPF.Play(); |
| 713 | + if (_settings.WMPInstalled) |
| 714 | + { |
| 715 | + _animationSoundWMP.Position = TimeSpan.Zero; |
| 716 | + _animationSoundWMP.Volume = _settings.SoundVolume / 100.0; |
| 717 | + _animationSoundWMP.Play(); |
| 718 | + } |
| 719 | + else |
| 720 | + { |
| 721 | + _animationSoundWPF.Play(); |
| 722 | + } |
715 | 723 | } |
716 | 724 | } |
717 | 725 |
|
@@ -1436,8 +1444,8 @@ protected virtual void Dispose(bool disposing) |
1436 | 1444 | { |
1437 | 1445 | _hwndSource?.Dispose(); |
1438 | 1446 | _notifyIcon?.Dispose(); |
1439 | | - animationSoundWMP?.Close(); |
1440 | | - animationSoundWPF?.Dispose(); |
| 1447 | + _animationSoundWMP?.Close(); |
| 1448 | + _animationSoundWPF?.Dispose(); |
1441 | 1449 | _viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged; |
1442 | 1450 | } |
1443 | 1451 |
|
|
0 commit comments