SoundPlayer won't play whole .wav file

I am trying to play wav files sequentially using SoundPlayer. I am using the PlaySync method. According to the documentation, program execution should not continue until the wav has finished. I have a test case with a messageBox call after PlaySync. The wav only plays for half a minute to a minute of any song before it stops and shows the MessageBox. I would like to avoid using WMP; I want to keep resources to a minimum. I appreciate any help.

An event that would let me know that the wav is done playing would be fine, too.

Here is my test case:

SoundPlayer SP;

private void Form1_Load(object sender, EventArgs e) {
SP = new SoundPlayer();
}//Form1_Load

private void button2_Click(object sender, EventArgs e) {
SP.Stop();
}//button2_Click

private void chkListBox1_SelectedIndexChanged(object sender, EventArgs e) {
SP.SoundLocation = chkListBox1.SelectedItem.ToString();
SP.Load ();
SP.PlaySync();
MessageBox.Show("Finished Playing");
}//chkListBox1_SelectedIndexChanged



Answer this question

SoundPlayer won't play whole .wav file

  • Minh

    Thank you for the reply.

    How do you notify that playing is complete


  • LoinHrat

    Does this happen with any WAV file How big is the WAV file you are using

  • blackpanter

    Nope, still won't work. Even in the background thread it plays for roughly the same amount of time before it stops and the completed event is raised. Here is the code I added:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
    SP.SoundLocation = e.Argument.ToString();
    SP.Load ();
    SP.PlaySync();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    MessageBox.Show("Finished Playing");
    }

    I thought perhaps there might be a limit to the size of the wav file, but I haven't found any documentation to support that theory. Any other thoughts


  • Frode Sorhoy

    I'm not familiar with Background/Worker objects. I'm going to learn what I can about them and try it out. Based on the name, I'm assuming that a background worker launches another thread (in the background) to alleviate the UI thread. If it is the UI thread that is unblocking .PlaySync, this should work.

    One note, the amount of time the wav file plays is inconsistent. Shouldn't the timeout be constant Or, perhaps the timeout is dependant on the other processes that are running. Anyway, I will give this a try.

    This will probably be the solution. Thank you for your help.


  • Jason Poll

    If SoundPlayer.SoundLocation is a local file, SoundPlayer.Load/LoadSync doesn't do anything but check that the file exists. There may be some limitation in the system when accessing a file (I can't think of any, SoundPlayer.Play[Sync] just delegates to Platform PlaySound(); which has not such documented limits). You could try setting the Stream property instead

    SP.Stream = new FileStream(e.Argument.ToString(), FileMode.Open, FileAccess.Read);
    SP.LoadSync();
    SP.PlaySync();



  • rico81

    As far as I can tell, SoundPlayer doesn't have any events to tell you when playing is complete, and there's no properties to tell you that a sound is playing.

    The best bet is to use SoundPlayer.PlaySync() on a background thread that raises an event when playing is complete.

    If you're using .NET 2 (VS 2005) then this can easily be done with a BackgroundWorker object. The event handler for the BackgroundWorker.DoWork event would call SoundPlayer.Load and/or SoundPlayer.PlaySync(). When SoundPlayer.PlaySync stops blocking the BackgroundWorker.RunWorkerCompleted event will fire back to the UI thread signalling that the playing is complete.

    The drawback of doing that is you don't reliable means of cancelling the sound playing.



  • Chad Buher

    You shouldn't be blocking the UI thread that long. If I remember correctly, there is code either in the framework or Visual Studio that will cancel a thread if it thinks the UI thread is blocked for too long.

    You should be using SoundPlayer.LoadAsync (and handling the LoadCompleted event) and SoundPlayer.Play. Or call SoundPlayer.PlaySync on a background thread and notify the UI thread when the playing is complete.

    The UI thread will have to manage the state of the SoundPlayer object and update controls appropriately.



  • SoundPlayer won't play whole .wav file