SoundsController.cs
3.5 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
using OnlineStore.Common;
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;
namespace DeviceLibrary
{
/// <summary>
/// 声音控制类
/// </summary>
public class SoundsController
{
private static string openDoorFile = @"\sounds\doorOpen.wav";
private static string closeDoorFile = @"\sounds\doorClose.wav";
private static string string_Filling = @"\sounds\string_filling.wav";
private static SoundPlayer alarmPlayer = new SoundPlayer();
private static bool InPlay = false;
private static string currFile = "";
public static void StartOpen()
{
StartPlay(openDoorFile);
}
public static void StartPlayBack()
{
LogUtil.info($"开始播放:{string_Filling},{File.Exists(Application.StartupPath + string_Filling)}");
StartPlay(string_Filling);
}
public static void StartClose() {
StartPlay(closeDoorFile);
}
private static void StartPlay(string fileName)
{
if (!Setting_Init.Enable_DoorSound)
{
return;
}
if (InPlay)
{
if (currFile == fileName)
{
return;
}
else
{
StopPlay();
}
}
// 音频文件路径
string audioFilePath = Application.StartupPath + fileName;
currFile = fileName;
LogUtil.info("开始播放:" + currFile);
try
{
if (File.Exists(audioFilePath))
{
// 设置要播放的音频文件路径
alarmPlayer.SoundLocation = audioFilePath;
// 播放音频
alarmPlayer.PlayLooping();
}
else
{
LogUtil.error("未找到音频文件:" + audioFilePath);
}
InPlay = true;
}
catch (Exception ex)
{
LogUtil.error("发生错误:" + ex.Message);
}
}
public static void StopPlay()
{
InPlay = false;
LogUtil.info("停止播放音频:"+currFile);
// 停止播放音频
alarmPlayer.Stop();
}
public static void PlayFile(string filename)
{
if (!Setting_Init.Enable_DoorSound)
{
return;
}
if (InPlay)
{
//报警中不播放。
return;
}
// 音频文件路径
string audioFilePath = Application.StartupPath + filename;
try
{
if (File.Exists(audioFilePath))
{
// 设置要播放的音频文件路径
alarmPlayer.SoundLocation = audioFilePath;
// 播放音频
alarmPlayer.Play();
}
else
{
LogUtil.error("未找到音频文件:" + audioFilePath);
}
}
catch (Exception ex)
{
LogUtil.error("发生错误:" + ex.Message);
}
}
}
}