Since this week is a holiday week, I wanted to post the Little Class a little early. This week I will show a class that will allow you to play a *.wav file easily using the System.Media.SoundPlayer class. This class can load the wave file on the same thread as the UI or you can load it asynchronously. Playing of the file can also be done on the same thread as the UI or on a separate thread.
To get this example working on your machine, you will need to place a *.wav file in the binary directory and name it "test.wav".
1
using System;
2
using System.Media;
3
using System.IO;
4
5
namespace ALittleClass ...{
6
class Program ...{
7
static void Main ( string[] args ) ...{
8
// get the path of the calling assembly
9
string path = Path.GetDirectoryName(typeof(Program).Assembly.Location);
10
// create a stream to the file
11
FileStream fileStream = new FileStream ( Path.Combine ( path, "test.wav" ), FileMode.Open, FileAccess.Read );
12
// make sure the stream is closed and disposed of
13
using ( fileStream ) ...{
14
// create the sound player
15
SoundPlayer player = new SoundPlayer ( fileStream );
16
// you could just pass the path to the file as well in the constructor
17
// SoundPlayer player = new SoundPlayer ( Path.Combine ( path, "test.wav" ) );
18
// or you could use the default constructor
19
// SoundPlayer player = new SoundPlayer ( );
20
// if you use the default constructor, you need to specify the sound file location or stream
21
// player.SoundLocation = Path.Combine ( path, "test.wav" );
22
// or
23
// player.Stream = fileStream;
24
// you can also listen for the stream or file location changing using the following events
25
player.SoundLocationChanged += delegate ( object sender, EventArgs e ) ...{
26
// The location changed
27
};
28
29
player.StreamChanged += delegate ( object sender, EventArgs e ) ...{
30
// the stream changed
31
};
32
33
// when the wave finishes loading, we want to play the file. To do so, we will listen for the
34
// LoadCompleted event.
35
player.LoadCompleted += delegate ( object sender, System.ComponentModel.AsyncCompletedEventArgs e ) ...{
36
SoundPlayer sp = sender as SoundPlayer;
37
// check that the loading wasn't canceled
38
if ( !e.Cancelled ) ...{
39
// play the file on a new thread.
40
// Play will load the file if it isn't already loaded, but you obviously couldn't call play here
41
// without loading the file first since we are in the LoadCompleted event.
42
sp.Play ( );
43
// you can also play the wave on the same thread as the UI by using the following line
44
// sp.PlaySync ( );
45
// or if you want to play the file continuously use the next line
46
// sp.PlayLooping ( );
47
}
48
};
49
// set a timeout in milliseconds for loading the wave file.
50
player.LoadTimeout = 100;
51
// load the file
52
player.Load ( );
53
// you can also load the file asynchronously using the following line
54
//player.LoadAsync ( );
55
// wait for the user to press enter
56
Console.ReadLine ( );
57
// we can stop the file from playing now.
58
player.Stop ( );
59
}
60
}
61
}
62
}