I'm using the SoundPlayer
class to play WAV files in my WPF application. The files are short and are part of the application and are played in response to events that occur in the program. The user has no control over what sound is played and can't decide to play a sound on their own. I've also tried using the WPF MediaPlayer
control, but I had problems with that control. The SoundPlayer
is working great, but there's a problem.
Essentially, I need to keep a queue of sounds and play the sounds one after the other as they queue up. In some circumstances, I have to stop whatever sound is playing and play another sound instead. So I have two requirements which turn out to be mutually exclusive with the SoundPlayer
control:
To implement this, I created a class called SoundController
. This has a background thread and it uses the Thread
's Dispatcher
to queue up calls to a method that I use to play the sounds, using BeginInvoke
. The method raises an event in the SoundController
event before it calls SoundPlayer.PlaySync
to play the sound, and then it raises another event after PlaySync
returns.
In my UI thread, at the point where I need to stop the sound, I call the SoundController
class's Stop
method. This calls the SoundPlayer
's Stop
method to stop playing the sound. And here's where the problem happens. It turns out that SoundPlayer.Stop
does NOT stop the sound, but it waits for the sound to finish before returning.
This stops my GUI dead. While I could call it asynchronously, my GUI won't stop but the sound won't stop, either.
As I said, the WPF MediaPlayer didn't work for us and it was overkill, anyway. Is there any other alternative for playing sounds that will give me the ability to raise events, play the sound in a background thread, and stop the playback?
Edit 06/26/2012:
This has been out here for over a month and I've gotten no responses. So I guess there are no other alternatives to the SoundPlayer
control.
I'm going to post a new question coming at the problem from a different angle.
There were no responses to this question and I've moved beyond this. So, for anyone who's wondering, the alternatives are the MediaElement
WPF control and making WIN32 API calls.
So which alternative did I use? Neither.
It turned out that for me, I had to add a second thread to my SoundController
class. The second thread does not run a Dispatcher
; instead, it runs code I wrote in a loop:
AutoResetEvent
.AutoResetEvent
is raised, it starts a while loop that checks to see if a sound is to be played.There's a little bit more but it doesn't matter. In any event, this seems to work OK. Because the sounds are playing on a separate Thread
, the currently playing sound can be aborted from a different Thread
by calling the SoundPlayer.Stop
method.
EDIT:
Here is a stripped down version of my code. I removed some things that were particular to my program's requirements that had to do with a special sound. This shows how the basic process works.
private string NextSound { get; set; }
public AutoResetEvent PlayASoundFlag = new AutoResetEvent( false );
private Dictionary<string, SoundPlayer> Sounds { get; set; }
private object soundLocker = new object();
public string SoundPlaying { get; private set; }
public bool Stopping { get; set; }
public void PlaySound( string key, bool stopCurrentSound ) {
if ( !Sounds.ContainsKey( key ) )
throw new ArgumentException( string.Format( "Sound \"{0}\" does not exist", key ), "key" );
lock ( soundLocker ) {
NextSound = key;
if ( SoundPlaying != null && stopCurrentSound )
Sounds[ SoundPlaying ].Stop();
PlayASoundFlag.Set();
}
}
private void SoundController() {
do {
PlayASoundFlag.WaitOne();
while ( !Stopping && NextSound != null ) {
lock ( soundLocker ) {
SoundPlaying = NextSound;
NextSound = null;
}
Sounds[ SoundPlaying ].PlaySync();
lock ( soundLocker )
SoundPlaying = null;
}
} while ( !Stopping );
}
When your program starts up, the class loads keys & sound files into the Sounds Dictionary
. That way, all the user has to do is pass the key for the sound they want to play to the PlaySound
method.
If you wanted to, you could replace the NextSound
property with a Queue
. The PlaySound
method would have to enqueue the key of the sound to be played and the SoundController
thread would have to dequeue each key from the thread in the while
loop, exiting when there's nothing left in the Queue
.
Finally, when your program stops, you need to set the Stopping
flag to true & set the PlayASoundFlag so the code comes out of the loop. That will cause the SoundController
thread to stop.
I leave initializing the Dictionary
and starting the thread to you.