[Socket] Fix DuplicateAndClose (#3995)
[mono-project.git] / mcs / class / System / System.Media / Win32SoundPlayer.cs
blob42aeba7a80df6c3066eda52d530d9e94a7ef2867
1 //
2 // Mono.Audio.Win32SoundPlayer
3 //
4 // Authors:
5 // Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
29 using System;
30 using System.IO;
31 using System.Runtime.InteropServices;
33 namespace Mono.Audio
35 internal class Win32SoundPlayer : IDisposable
37 public Win32SoundPlayer (Stream s)
39 if (s != null) {
40 _buffer = new byte [s.Length];
41 s.Read (_buffer, 0, _buffer.Length);
42 } else {
43 _buffer = new byte [0];
47 [DllImport ("winmm.dll", SetLastError = true)]
48 static extern bool PlaySound (
49 byte [] ptrToSound,
50 UIntPtr hmod,
51 SoundFlags flags);
53 public Stream Stream {
54 set {
55 Stop ();
56 if (value != null) {
57 _buffer = new byte [value.Length];
58 value.Read (_buffer, 0, _buffer.Length);
59 } else {
60 _buffer = new byte [0];
65 public void Dispose ()
67 Dispose (true);
68 GC.SuppressFinalize (this);
71 ~Win32SoundPlayer ()
73 Dispose (false);
76 protected virtual void Dispose (bool disposing)
78 if (!_disposed) {
79 Stop ();
80 _disposed = true;
84 public void Play ()
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);
102 public void Stop ()
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;