Add lock for sound & Rename variable

This commit is contained in:
Jack251970 2025-09-30 19:41:27 +08:00
parent 5b6ea73513
commit d56d85b702

View file

@ -2,6 +2,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Media; using System.Media;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -61,8 +62,9 @@ namespace Flow.Launcher
private bool _isArrowKeyPressed = false; private bool _isArrowKeyPressed = false;
// Window Sound Effects // Window Sound Effects
private MediaPlayer animationSoundWMP; private MediaPlayer _animationSoundWMP;
private SoundPlayer animationSoundWPF; private SoundPlayer _animationSoundWPF;
private readonly Lock _soundLock = new();
// Window WndProc // Window WndProc
private HwndSource _hwndSource; private HwndSource _hwndSource;
@ -686,32 +688,38 @@ namespace Flow.Launcher
#region Window Sound Effects #region Window Sound Effects
private void InitSoundEffects() private void InitSoundEffects()
{
lock (_soundLock)
{ {
if (_settings.WMPInstalled) if (_settings.WMPInstalled)
{ {
animationSoundWMP?.Close(); _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?.Dispose();
animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); _animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
animationSoundWPF.Load(); _animationSoundWPF.Load();
}
} }
} }
private void SoundPlay() private void SoundPlay()
{
lock (_soundLock)
{ {
if (_settings.WMPInstalled) if (_settings.WMPInstalled)
{ {
animationSoundWMP.Position = TimeSpan.Zero; _animationSoundWMP.Position = TimeSpan.Zero;
animationSoundWMP.Volume = _settings.SoundVolume / 100.0; _animationSoundWMP.Volume = _settings.SoundVolume / 100.0;
animationSoundWMP.Play(); _animationSoundWMP.Play();
} }
else else
{ {
animationSoundWPF.Play(); _animationSoundWPF.Play();
}
} }
} }
@ -1436,8 +1444,8 @@ namespace Flow.Launcher
{ {
_hwndSource?.Dispose(); _hwndSource?.Dispose();
_notifyIcon?.Dispose(); _notifyIcon?.Dispose();
animationSoundWMP?.Close(); _animationSoundWMP?.Close();
animationSoundWPF?.Dispose(); _animationSoundWPF?.Dispose();
_viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged; _viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged;
} }