PTipSoundProcess.cs
4.8 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 PTipSoundProcess
{
/// <summary>
/// 第二屏幕是否显示,默认不显示
/// </summary>
public static bool ViewScreen_Show = false;
public static bool AOIopen = false;
//public static bool DisShowScreenAlarm = ConfigAppSettings.GetBoolValue(Setting_Init.DisShowScreenAlarm);
public static bool DisShowScreenAlarm = Setting_NInit.Work_DisShowScreenAlarm;
private static System.Timers.Timer proTimer = null;
private static bool InPlay = false;
//private static string soundFileName = @"\sound\alarm.wav";
// 创建一个新的 SoundPlayer 实例
private static SoundPlayer alarmPlayer = 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 (PTipSoundProcess.IsAlarm(out string msg))
{
if (InPlay)
{
if (!Setting_NInit.Set_AlarmSound)
{
StopPlay();
}
}
else
{
StartPlay();
}
}
else
{
if (InPlay)
{
StopPlay();
}
}
}
private static void StartPlay( )
{
if (!Setting_NInit.Set_AlarmSound)
{
return;
}
// 音频文件路径
string audioFilePath = Application.StartupPath+ Setting_NInit.Set_AlarmSoundFile;
LogUtil.info("开始播放:" + audioFilePath);
try
{
if(File.Exists(audioFilePath))
{
// 设置要播放的音频文件路径
alarmPlayer.SoundLocation = audioFilePath;
// 播放音频
alarmPlayer.PlayLooping();
}
else
{
LogUtil.error("未找到音频文件:" + audioFilePath);
}
InPlay = true;
}
catch (Exception ex)
{
LogUtil.error("发生错误:" + ex.Message);
}
}
private static void StopPlay()
{
InPlay = false;
LogUtil.info("停止播放音频");
// 停止播放音频
alarmPlayer.Stop();
}
public static void PlayFile(string filename)
{
if (!Setting_NInit.Set_TipSound)
{
return;
}
if (InPlay)
{
//报警中不播放。
return;
}
// 音频文件路径
string audioFilePath = Application.StartupPath + filename;
if (filename.Equals(Setting_NInit.Set_PointChangeSound))
{
}
else
{
LogUtil.info("开始播放:" + audioFilePath);
}
try
{
if (File.Exists(audioFilePath))
{
// 设置要播放的音频文件路径
alarmPlayer.SoundLocation = audioFilePath;
// 播放音频
alarmPlayer.Play();
}
else
{
LogUtil.error("未找到音频文件:" + audioFilePath);
}
}
catch (Exception ex)
{
LogUtil.error("发生错误:" + ex.Message);
}
}
}
}