2 // System.Media.SoundPlayer
5 // Paolo Molaro (lupus@ximian.com)
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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.
30 using System.Threading;
31 using System.Runtime.Serialization;
32 using System.ComponentModel;
35 namespace System.Media {
39 public class SoundPlayer: Component, ISerializable {
41 string sound_location;
43 object tag = String.Empty;
46 int load_timeout = 10000;
48 #region Only used for Alsa implementation
54 #region Only used for Win32 implementation
55 Win32SoundPlayer win32_player;
58 static readonly bool use_win32_player;
62 use_win32_player = (Environment.OSVersion.Platform != PlatformID.Unix);
67 sound_location = String.Empty;
70 public SoundPlayer (Stream stream): this ()
75 public SoundPlayer (string soundLocation): this ()
77 if (soundLocation == null)
78 throw new ArgumentNullException ("soundLocation");
79 sound_location = soundLocation;
82 protected SoundPlayer (SerializationInfo serializationInfo, StreamingContext context): this ()
84 throw new NotImplementedException ();
87 void LoadFromStream (Stream s)
89 mstream = new MemoryStream ();
90 byte[] buf = new byte [4096];
92 while ((count = s.Read (buf, 0, 4096)) > 0) {
93 mstream.Write (buf, 0, count);
98 void LoadFromUri (string location)
102 if (string.IsNullOrEmpty (location))
105 if(File.Exists(location))
106 data = new FileStream(location, FileMode.Open, FileAccess.Read, FileShare.Read);
108 System.Net.WebRequest request = System.Net.WebRequest.Create(location);
109 data = request.GetResponse().GetResponseStream();
113 LoadFromStream (data);
118 // can this be reused to load the same file again without re-setting the location?
121 if (audiostream != null) {
122 LoadFromStream (audiostream);
124 LoadFromUri (sound_location);
127 // force recreate for new stream
131 load_completed = true;
132 AsyncCompletedEventArgs e = new AsyncCompletedEventArgs (null, false, this);
134 if (LoadCompleted != null)
135 LoadCompleted (this, e);
137 if (use_win32_player) {
138 if (win32_player == null)
139 win32_player = new Win32SoundPlayer (mstream);
141 win32_player.Stream = mstream;
145 void AsyncFinished (IAsyncResult ar)
147 ThreadStart async = ar.AsyncState as ThreadStart;
148 async.EndInvoke (ar);
151 public void LoadAsync ()
155 ThreadStart async = new ThreadStart (Load);
156 async.BeginInvoke (AsyncFinished, async);
159 protected virtual void OnLoadCompleted (AsyncCompletedEventArgs e)
163 protected virtual void OnSoundLocationChanged (EventArgs e)
167 protected virtual void OnStreamChanged (EventArgs e)
173 if (!use_win32_player) {
176 adata.IsStopped = false;
184 if (!use_win32_player) {
185 ThreadStart async = new ThreadStart (PlaySync);
186 async.BeginInvoke (AsyncFinished, async);
190 if (mstream == null) {
191 SystemSounds.Beep.Play ();
195 win32_player.Play ();
199 private void PlayLoop ()
203 if (mstream == null) {
204 SystemSounds.Beep.Play ();
212 public void PlayLooping ()
214 if (!use_win32_player) {
215 ThreadStart async = new ThreadStart (PlayLoop);
216 async.BeginInvoke (AsyncFinished, async);
220 if (mstream == null) {
221 SystemSounds.Beep.Play ();
225 win32_player.PlayLooping ();
229 public void PlaySync ()
233 if (mstream == null) {
234 SystemSounds.Beep.Play ();
238 if (!use_win32_player) {
241 adata = new WavData (mstream);
243 adev = AudioDevice.CreateDevice (null);
251 win32_player.PlaySync ();
257 if (!use_win32_player) {
260 adata.IsStopped = true;
262 win32_player.Stop ();
266 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
270 public bool IsLoadCompleted {
272 return load_completed;
276 public int LoadTimeout {
282 throw new ArgumentException ("timeout must be >= 0");
283 load_timeout = value;
287 public string SoundLocation {
289 return sound_location;
293 throw new ArgumentNullException ("value");
294 sound_location = value;
295 load_completed = false;
296 OnSoundLocationChanged (EventArgs.Empty);
297 if (SoundLocationChanged != null)
298 SoundLocationChanged (this, EventArgs.Empty);
302 public Stream Stream {
307 if (audiostream != value) {
309 load_completed = false;
310 OnStreamChanged (EventArgs.Empty);
311 if (StreamChanged != null)
312 StreamChanged (this, EventArgs.Empty);
326 public event AsyncCompletedEventHandler LoadCompleted;
327 public event EventHandler SoundLocationChanged;
328 public event EventHandler StreamChanged;