0

I am trying to play a song in the background of my windows form aplication. I tried this:

 System.Media.SoundPlayer sound = new System.Media.SoundPlayer();
 sound.Stream = Properties.Resources.K_39_NAAN___Wavin_39_Flag;
 sound.PlayLooping();

But when i do this, When my application runs for an x number of minutes and i get this error:

vshost32.exe has stopped working

I cant just use an url because it has to be installed on other computers and work.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
Roboneter
  • 867
  • 2
  • 13
  • 24
  • Yes it does play. But after a few minutes i get the error. **vshost32.exe has stopped working** – Roboneter Dec 06 '13 at 12:26
  • @user3074355 Try adding `sound.LoadAsync();` before `sound.PlayLooping();` –  Dec 06 '13 at 12:27
  • What is the scope of your `sound` object? If the lifetime of that object is just a single method you may find it is being cleaned up - try declaring it as a field instead? – Dan Puzey Dec 06 '13 at 12:31
  • @Tijesunimi Still the same error. – Roboneter Dec 06 '13 at 12:32
  • @Marco Luizinho Still the same error. – Roboneter Dec 06 '13 at 12:33
  • @DanPuzey How do i do this? – Roboneter Dec 06 '13 at 12:33
  • @user3074355: I suspect the code you posted is from a single method. You need to move your declaration of the `sound` variable out of the method so that it has a longer lifetime (perhaps declare it as a field of your main window instead). – Dan Puzey Dec 06 '13 at 12:42
  • @DanPuzey The code i posted is in the form_load function... – Roboneter Dec 06 '13 at 12:55
  • ... so you need to move your declaration of the sound variable out of the method so that it has a longer lifetime (perhaps declare it as a field of your main window instead). – Dan Puzey Dec 06 '13 at 13:00
  • @DanPuzey Do you have an example code of how to do this? – Roboneter Dec 06 '13 at 13:09
  • I'm sorry, but if you need help creating a field then your needs will be better served by a basic WinForms programming tutorial than by asking questions on StackOverflow. – Dan Puzey Dec 06 '13 at 13:30
  • Thanks ! My problem is fixed. Declaration above the public form1() And playlooping insinde the form_load() Thanks a lot – Roboneter Dec 06 '13 at 13:33

0 Answers0