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;
@ -687,31 +689,37 @@ namespace Flow.Launcher
private void InitSoundEffects() private void InitSoundEffects()
{ {
if (_settings.WMPInstalled) lock (_soundLock)
{ {
animationSoundWMP?.Close(); if (_settings.WMPInstalled)
animationSoundWMP = new MediaPlayer(); {
animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav")); _animationSoundWMP?.Close();
} _animationSoundWMP = new MediaPlayer();
else _animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav"));
{ }
animationSoundWPF?.Dispose(); else
animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav"); {
animationSoundWPF.Load(); _animationSoundWPF?.Dispose();
_animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
_animationSoundWPF.Load();
}
} }
} }
private void SoundPlay() private void SoundPlay()
{ {
if (_settings.WMPInstalled) lock (_soundLock)
{ {
animationSoundWMP.Position = TimeSpan.Zero; if (_settings.WMPInstalled)
animationSoundWMP.Volume = _settings.SoundVolume / 100.0; {
animationSoundWMP.Play(); _animationSoundWMP.Position = TimeSpan.Zero;
} _animationSoundWMP.Volume = _settings.SoundVolume / 100.0;
else _animationSoundWMP.Play();
{ }
animationSoundWPF.Play(); else
{
_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;
} }