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.
33 using System.Runtime.InteropServices;
37 internal class Win32SoundPlayer : IDisposable
39 public Win32SoundPlayer (Stream s)
42 _buffer = new byte [s.Length];
43 s.Read (_buffer, 0, _buffer.Length);
45 _buffer = new byte [0];
49 [DllImport ("winmm.dll", SetLastError = true)]
50 static extern bool PlaySound (
55 public Stream Stream {
59 _buffer = new byte [value.Length];
60 value.Read (_buffer, 0, _buffer.Length);
62 _buffer = new byte [0];
67 public void Dispose ()
70 GC.SuppressFinalize (this);
78 protected virtual void Dispose (bool disposing)
88 PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_ASYNC |
89 SoundFlags.SND_MEMORY);
92 public void PlayLooping ()
94 PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_MEMORY |
95 SoundFlags.SND_LOOP | SoundFlags.SND_ASYNC);
98 public void PlaySync ()
100 PlaySound (_buffer, UIntPtr.Zero, SoundFlags.SND_SYNC |
101 SoundFlags.SND_MEMORY | SoundFlags.SND_NODEFAULT);
106 PlaySound ((byte []) null, UIntPtr.Zero, SoundFlags.SND_SYNC);
109 enum SoundFlags : uint
111 SND_SYNC = 0x0000, // play synchronously
112 SND_ASYNC = 0x0001, // play asynchronously
113 SND_NODEFAULT = 0x0002, // do not play default sound if sound not found
114 SND_MEMORY = 0x0004, // pszSound is loaded in memory
115 SND_LOOP = 0x0008, // play repeatedly until next PlaySound
116 SND_FILENAME = 0x00020000, // pszSound is a file name
119 private byte [] _buffer;
120 private bool _disposed;