hi guys! how can I have the Event or what is the event raised when my SoundPlayer is done playing the sound? Thanks in advance!
Printable View
hi guys! how can I have the Event or what is the event raised when my SoundPlayer is done playing the sound? Thanks in advance!
There isn't one. If that's important to you then rather than calling Play and implicitly creating a worker thread to play the sound, you could explicitly create a worker thread and call PlaySync instead. When PlaySync returns the sound has finished playing. If you use a BackgroundWorker you can call its RunWorkerAsync method, then create a SoundPlayer and call its PlaySync method in the DoWork event handler. The BackgroundWorker's RunWorkerCompleted event will be raised when the sound has finished playing.Code:private void Form1_Load(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync("WAV file path here");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
using (System.Media.SoundPlayer sp = new System.Media.SoundPlayer((string)(e.Argument)))
{
sp.PlaySync();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("The sound has finished playing");
}