1

I am trying to implement a simple game in .NET with C#. There is a ball bouncing against objects, and when it bounces I play a sound asynchronously using System.Media:SoundPlayer. The problem is that most of the time (not always) the ball freezes at the time of impact, for as long as the sound plays.

This happens on Windows XP, but not on Windows 7. I use .NET 3.5. And the weirdest thing is that the problem disappears if I open the application Windows Media Player and play a few seconds of an mp3.

Here's the code where I initialize the SoundPlayer object:

System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("Game.Resources.GameBounce.wav");
_playerBounce = new System.Media.SoundPlayer(s);
_playerBounce.Load();

...and when the ball hits an obstacle, this line is called: _playerBounce.Play();

I have also tried "Attempt #4" in this post: How to use System.Media.SoundPlayer to asynchronously play a sound file?

Another desperate attempt, based on some advice I found somewhere on the net, was to save the sound stream to a file, that SoundPlayer works better when initialized from a file.

Neither of those attempts worked.

Any ideas?

Community
  • 1
  • 1
  • could this be because you are assigning the new reference of a SoundPlayer to a field and not disposing it after it is not needed? – Marek Aug 26 '10 at 11:00
  • How big (Kb/MB) is the wav file? – Tony Abrams Aug 26 '10 at 12:22
  • I tried "Attempt #4" in the linked post, and in that example the SoundPlayer is instantiated for each bounce, with a using directive. The WAV file is 6 kB, 8 bit mono, 11.025 kHz. – Hakan Eriksson Aug 27 '10 at 11:31

0 Answers0