using System;
using System.IO;
using System.Media;
namespace 報到系統客戶端.Services
{
///
/// 通知音效服務,密集呼叫時會中斷先前的播放再重新播放。
///
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();
}
///
/// 播放 ding.wav;若有其他音效正在播放,先全部停止再播放。
///
public static void Ding() => PlaySound(_dingWavePath, ref _dingPlayer);
///
/// 播放 CheckinOK.wav;若有其他音效正在播放,先全部停止再播放。
///
public static void CheckinOK() => PlaySound(_checkinWavePath, ref _checkinPlayer);
///
/// 播放 CheckoutOK.wav;若有其他音效正在播放,先全部停止再播放。
///
public static void CheckoutOK() => PlaySound(_checkoutWavePath, ref _checkoutPlayer);
}
}