DXSound.cs
8.61 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
///--------------------------------------------------------------------------
/// Copyright (C) 2010 长沙中天电子设计开发有限公司版权所有。
///
/// 文 件 名:DXSound.cs
/// 功能描述:音效模块
///
/// 使用方法:
/// 用LoadSound()方法加载音效文件
/// 播放时调用Play()方法
/// 停止调用Stop()或StopAll()方法
///
/// 注意:写new DXSound()代码时,取消代码调试信息:Ctrl+Alt+E,修改Managed Debuggin Assistants->LoaderLock 的选中状态去掉
///
/// 创建标识:杨斌 2010-08-05
/// 修改:杨斌 2012-06-18 加入mp3格式
///
///--------------------------------------------------------------------------
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;
using System.Collections.Specialized;
using GeneralLib;
namespace GeneralLib
{
public class DXSound
{
private Microsoft.DirectX.DirectSound.Device dev;
private OrderedDictionary sndKeys;
/// <summary>
/// 是否初始化成功
/// </summary>
private bool initOK = false;
public bool InitOK
{
get { return initOK; }
}
/// <summary>
/// 说明:构造函数
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="owner">使用声音设备的程序的控件或窗体</param>
public DXSound()
{
sndKeys = new OrderedDictionary();
}
/// <summary>
/// 说明:初始化音效
/// 创建:杨斌 2010-09-08
/// </summary>
/// <param name="owner">所有者控件</param>
/// <returns></returns>
public bool InitSound(Control owner)
{
initOK = false;
try
{
dev = new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel(owner.Handle, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);
//dev.SetCooperativeLevel((IntPtr)0, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);
initOK = true;
}
catch (Exception ex)
{
SystemLog.WriterLog(ex, false);
}
return initOK;
}
/// <summary>
/// 说明:析构函数
/// 创建:杨斌 2010-07-31
/// </summary>
~DXSound()
{
//this.StopAll();
if (dev != null) dev.Dispose();//必须手动释放
}
/// <summary>
/// 获取播放文件格式
/// </summary>
/// <param name="filePath"></param>
/// <returns>小写,如wav、mp3</returns>
private string GetSoundFormat(string filePath)
{
string res = "";
try
{
int i = filePath.LastIndexOf(".");
if (i >= 0)
res = filePath.Substring(i + 1);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
return res;
}
/// <summary>
/// 说明:加载声音文件
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
/// <param name="filePath">文件路径</param>
/// <param name="mixCount">混音个数</param>
/// <returns>是否成功</returns>
public bool LoadSound(string key, string filePath, int mixCount)
{
bool loadOK = false;
try
{
OrderedDictionary sndBufs = new OrderedDictionary();
sndKeys.Add(key, sndBufs);
sndBufs.Add("0", 0);//用来保存当前播放的缓冲索引
if (dev != null)//若声卡初始化失败,dev==null,添加缓冲字典会报错,杨斌 2012-03-07
{
for (int i = 1; i <= mixCount; i++)
{
Microsoft.DirectX.DirectSound.SecondaryBuffer sndBuf = new Microsoft.DirectX.DirectSound.SecondaryBuffer(filePath, dev);
sndBufs.Add(i.ToString(), sndBuf);
}
}
loadOK = true;
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
return loadOK;
}
/// <summary>
/// 移除声音
/// </summary>
/// <param name="key">声音键值</param>
public void RemoveSound(string key)
{
if (!sndKeys.Contains(key)) return;
OrderedDictionary sndBufs = (OrderedDictionary)sndKeys[key];
for (int i = 1; i < sndBufs.Count; i++)
{
Microsoft.DirectX.DirectSound.SecondaryBuffer sndBuf;
try
{
sndBuf = (Microsoft.DirectX.DirectSound.SecondaryBuffer)sndBufs[i];
sndBuf.Stop();
sndBuf.Dispose();
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
finally
{
sndBuf = null;
}
}
sndKeys.Remove(key);
sndBufs = null;
}
/// <summary>
/// 说明:加载声音文件
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
/// <param name="filePath">文件路径</param>
/// <returns>是否成功</returns>
public bool LoadSound(string key, string filePath)
{
return LoadSound(key, filePath, 1);
}
/// <summary>
/// 说明:播放声音
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
/// <param name="isLoop">是否循环播放</param>
private void PlaySond(string key, bool isLoop)
{
if (!sndKeys.Contains(key)) return;
OrderedDictionary sndBufs = (OrderedDictionary)sndKeys[key];
if (sndBufs.Count < 2) return;
int i = (int)sndBufs[0] + 1;//当前播放的缓冲索引+1
if (i > sndBufs.Count - 1) i = sndBufs.Count - 1;
try
{
Microsoft.DirectX.DirectSound.SecondaryBuffer sndBuf = (Microsoft.DirectX.DirectSound.SecondaryBuffer)sndBufs[i];
sndBuf.Stop();
sndBuf.SetCurrentPosition(0);
sndBuf.Play(0, isLoop ? Microsoft.DirectX.DirectSound.BufferPlayFlags.Looping : Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex, false);
}
finally
{
sndBufs[0] = i;
}
}
/// <summary>
/// 说明:播放声音
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
public void Play(string key)
{
PlaySond(key, false);
}
/// <summary>
/// 说明:循环播放声音
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
public void PlayLoop(string key)
{
PlaySond(key, true);
}
/// <summary>
/// 说明:停止声音
/// 创建:杨斌 2010-07-31
/// </summary>
/// <param name="key">声音键值</param>
public void Stop(string key)
{
if (!sndKeys.Contains(key)) return;
OrderedDictionary sndBufs = (OrderedDictionary)sndKeys[key];
for (int i = 1; i < sndBufs.Count; i++)
{
try
{
Microsoft.DirectX.DirectSound.SecondaryBuffer sndBuf = (Microsoft.DirectX.DirectSound.SecondaryBuffer)sndBufs[i];
sndBuf.Stop();
sndBuf.SetCurrentPosition(0);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex, false);
}
finally
{
sndBufs[0] = 0;//当前播放的缓冲索引清零
}
}
}
/// <summary>
/// 说明:停止所有声音
/// 创建:杨斌 2010-07-31
/// </summary>
public void StopAll()
{
foreach (string key in sndKeys.Keys)
{
Stop(key);
}
}
}
}