2 // Mono.Audio.Win32SoundPlayer
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2007 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Runtime
.InteropServices
;
35 internal class Win32SoundPlayer
: IDisposable
37 public Win32SoundPlayer (Stream s
)
40 _buffer
= new byte [s
.Length
];
41 s
.Read (_buffer
, 0, _buffer
.Length
);
43 _buffer
= new byte [0];
47 [DllImport ("winmm.dll", SetLastError
= true)]
48 static extern bool PlaySound (
53 public Stream Stream
{
57 _buffer
= new byte [value.Length
];
58 value.Read (_buffer
, 0, _buffer
.Length
);
60 _buffer
= new byte [0];
65 public void Dispose ()
68 GC
.SuppressFinalize (this);
76 protected virtual void Dispose (bool disposing
)
86 PlaySound (_buffer
, UIntPtr
.Zero
, SoundFlags
.SND_ASYNC
|
87 SoundFlags
.SND_MEMORY
);
90 public void PlayLooping ()
92 PlaySound (_buffer
, UIntPtr
.Zero
, SoundFlags
.SND_MEMORY
|
93 SoundFlags
.SND_LOOP
| SoundFlags
.SND_ASYNC
);
96 public void PlaySync ()
98 PlaySound (_buffer
, UIntPtr
.Zero
, SoundFlags
.SND_SYNC
|
99 SoundFlags
.SND_MEMORY
| SoundFlags
.SND_NODEFAULT
);
104 PlaySound ((byte []) null, UIntPtr
.Zero
, SoundFlags
.SND_SYNC
);
107 enum SoundFlags
: uint
109 SND_SYNC
= 0x0000, // play synchronously
110 SND_ASYNC
= 0x0001, // play asynchronously
111 SND_NODEFAULT
= 0x0002, // do not play default sound if sound not found
112 SND_MEMORY
= 0x0004, // pszSound is loaded in memory
113 SND_LOOP
= 0x0008, // play repeatedly until next PlaySound
114 SND_FILENAME
= 0x00020000, // pszSound is a file name
117 private byte [] _buffer
;
118 private bool _disposed
;