001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Text;
005.
using
System.Runtime.InteropServices;
006.
using
System.IO;
007.
008.
namespace
DDSoundEngine
009.
{
010.
class
SimplePlaySound : IDisposable
011.
{
012.
byte
[] Sbuffer;
013.
014.
string
path;
015.
016.
bool
_buffer;
017.
018.
public
SimplePlaySound(
string
fileName,
bool
buffer)
019.
{
020.
if
(!System.IO.File.Exists(fileName))
021.
{
022.
throw
new
NotImplementedException(
"Error: File Name Incorrect"
);
023.
}
024.
025.
_buffer = buffer;
026.
027.
if
(buffer)
028.
{
029.
using
(FileStream s =
new
FileStream(fileName, FileMode.Open))
030.
{
031.
Sbuffer =
new
byte
[s.Length];
032.
s.Read(Sbuffer, 0, (
int
)s.Length);
033.
}
034.
}
035.
else
036.
{
037.
path = fileName;
038.
}
039.
}
040.
041.
[DllImport(
"winmm.dll"
, SetLastError =
true
, CallingConvention = CallingConvention.Winapi)]
042.
static
extern
bool
PlaySound(
string
pszSound,
043.
IntPtr hMod, SoundFlags sf);
044.
045.
[DllImport(
"winmm.dll"
, SetLastError =
true
)]
046.
static
extern
bool
PlaySound(
byte
[] pszSound, IntPtr hmod, SoundFlags fdwSound);
047.
048.
[DllImport(
"winmm.dll"
, SetLastError =
true
, CallingConvention = CallingConvention.Winapi)]
049.
static
extern
bool
sndPlaySound(IntPtr ptr,
int
fuSound);
050.
051.
052.
[Flags]
053.
public
enum
SoundFlags :
int
054.
{
055.
SND_SYNC = 0x0000,
056.
SND_ASYNC = 0x0001,
057.
SND_NODEFAULT = 0x0002,
058.
SND_MEMORY = 0x0004,
059.
SND_LOOP = 0x0008,
060.
SND_NOSTOP = 0x0010,
061.
SND_NOWAIT = 0x00002000,
062.
SND_ALIAS = 0x00010000,
063.
SND_ALIAS_ID = 0x00110000,
064.
SND_FILENAME = 0x00020000,
065.
SND_RESOURCE = 0x00040004,
066.
SND_PURGE = 0x0040
067.
}
068.
069.
public
void
Play()
070.
{
071.
if
(_buffer)
072.
{
073.
PlaySound(Sbuffer, IntPtr.Zero, SoundFlags.SND_MEMORY | SoundFlags.SND_ASYNC);
074.
}
075.
else
076.
{
077.
PlaySound(path,
new
System.IntPtr(), SoundFlags.SND_SYNC);
078.
}
079.
}
080.
081.
public
void
PlayLoop()
082.
{
083.
if
(_buffer)
084.
{
085.
PlaySound(Sbuffer, IntPtr.Zero, SoundFlags.SND_MEMORY | SoundFlags.SND_ASYNC | SoundFlags.SND_LOOP);
086.
}
087.
else
088.
{
089.
PlaySound(path,
new
System.IntPtr(), SoundFlags.SND_SYNC | SoundFlags.SND_LOOP);
090.
}
091.
}
092.
093.
public
void
Stop()
094.
{
095.
if
(_buffer)
096.
{
097.
PlaySound((
string
)
null
, IntPtr.Zero , 0);
098.
}
099.
else
100.
{
101.
PlaySound((
string
)
null
, IntPtr.Zero , 0);
102.
}
103.
}
104.
105.
#region Dispose
106.
107.
108.
109.
public
void
Dispose()
110.
{
111.
Dispose(
true
);
112.
113.
114.
115.
116.
117.
GC.SuppressFinalize(
this
);
118.
}
119.
120.
121.
122.
123.
private
bool
disposed =
false
;
124.
125.
126.
127.
128.
129.
130.
131.
132.
private
void
Dispose(
bool
disposing)
133.
{
134.
135.
if
(!
this
.disposed)
136.
{
137.
138.
139.
if
(disposing)
140.
{
141.
142.
PlaySound((
string
)
null
, IntPtr.Zero, 0);
143.
144.
145.
}
146.
147.
disposed =
true
;
148.
149.
}
150.
}
151.
#endregion
152.
153.
154.
}
155.
}