1

I'm currently using this method to play a .wav file:

private void button5_Click(object sender, System.EventArgs e)
{
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Song1);
    player.PlaySync();
}

It's playing the .wav file for like a second, then it stops and a few seconds later after stopping, the program freezes and closes itself without showing any errors.

Did anyone experience this too and if so, (how) did you solve it?

  • What are you doing after you call `Play`? This method is asynchronous, meaning that the sound will continue playing but the method returns. Either use `PlaySync` or make sure you don't exit the program afterwards. – PMF Dec 30 '21 at 14:22
  • Okay so, `PlaySync` seems to work and it plays the song, BUT the window freezes instantly and I can't click anything if I clicked on the button which plays the song. – Scrooge McDuck Dec 30 '21 at 14:28
  • Yes, that's expected. As the name says, `PlaySync` is synchronous, meaning it won't return until the sound has played. If you show a bit more code around the place where you start the sound, we may be able to help you do it the right way. – PMF Dec 30 '21 at 14:34
  • Basically the code is just in a simple button click method. I've put the whole method in the question. – Scrooge McDuck Dec 30 '21 at 14:41
  • Hmmm... I guess `Play` is then failing because the player runs out of scope. Try making the player a member of the class instead of a local variable. Then you should be able to keep the sound playing. – PMF Dec 30 '21 at 14:45

0 Answers0