AlarmProcess.cs
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using static TSA_V.Common.LogUtil;
namespace TSA_V
{
public class AlarmProcess
{
private static System.Timers.Timer proTimer = null;
private static bool InPlay = false;
private static string soundFileName = @"\sound\alarm.wav";
// 创建一个新的 SoundPlayer 实例
private static SoundPlayer player = new SoundPlayer();
public static void Init()
{
if (proTimer == null)
{
proTimer = new System.Timers.Timer();
proTimer.AutoReset = true;
proTimer.Enabled = false;
proTimer.Interval = 1000;
proTimer.Elapsed += ProTimer_Elapsed;
proTimer.Start();
}
}
public static bool IsAlarm(out string msg)
{
msg = "";
if (!String.IsNullOrEmpty(TSAVBean.WarnMsg))
{
msg = TSAVBean.WarnMsg;
return true;
}
else if(TSAVBean.NoAirAlarm)
{
msg = ResourceControl.GetString(ResourceControl.NoAirAlarm, "未检测到气压信号"); ;
return true;
}
return false;
}
private static void ProTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (AlarmProcess.IsAlarm(out string msg))
{
if (InPlay)
{
}
else
{
StartPlay();
}
}
else
{
if (InPlay)
{
StopPlay();
}
}
}
public static void StartPlay( )
{
// 音频文件路径
string audioFilePath = Application.StartupPath+ soundFileName;
LogUtil.info("开始播放:" + audioFilePath);
try
{
if(File.Exists(audioFilePath))
{
// 设置要播放的音频文件路径
player.SoundLocation = audioFilePath;
// 播放音频
player.PlayLooping();
}
else
{
LogUtil.error("未找到音频文件:" + audioFilePath);
}
InPlay = true;
}
catch (Exception ex)
{
LogUtil.error("发生错误:" + ex.Message);
}
}
public static void StopPlay()
{
InPlay = false;
LogUtil.info("停止播放音频");
// 停止播放音频
player.Stop();
}
}
}