Files
thinkyu/報到系統客戶端/Services/NotificationSoundService.cs
T
sryang 577060bc78 chore: 首次簽入 Thinkyu ASP.NET 專案
- 加入 Visual Studio / ASP.NET .gitignore
- 排除建置輸出、IDE 設定、NuGet packages、大型 MSI 安裝檔
2026-09-10 09:42:37 +08:00

58 lines
2.0 KiB
C#

using System;
using System.IO;
using System.Media;
namespace 報到系統客戶端.Services
{
/// <summary>
/// 通知音效服務,密集呼叫時會中斷先前的播放再重新播放。
/// </summary>
public static class NotificationSoundService
{
private static readonly string _dingWavePath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sound\\ding.wav");
private static readonly string _checkinWavePath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sound\\CheckinOK.wav");
private static readonly string _checkoutWavePath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sound\\CheckoutOK.wav");
private static SoundPlayer _dingPlayer;
private static SoundPlayer _checkinPlayer;
private static SoundPlayer _checkoutPlayer;
private static void StopAll()
{
_dingPlayer?.Stop();
_checkinPlayer?.Stop();
_checkoutPlayer?.Stop();
}
private static void PlaySound(string path, ref SoundPlayer player)
{
if (!File.Exists(path))
return;
if (player == null)
player = new SoundPlayer(path);
StopAll();
player.Play();
}
/// <summary>
/// 播放 ding.wav;若有其他音效正在播放,先全部停止再播放。
/// </summary>
public static void Ding() => PlaySound(_dingWavePath, ref _dingPlayer);
/// <summary>
/// 播放 CheckinOK.wav;若有其他音效正在播放,先全部停止再播放。
/// </summary>
public static void CheckinOK() => PlaySound(_checkinWavePath, ref _checkinPlayer);
/// <summary>
/// 播放 CheckoutOK.wav;若有其他音效正在播放,先全部停止再播放。
/// </summary>
public static void CheckoutOK() => PlaySound(_checkoutWavePath, ref _checkoutPlayer);
}
}