SoundPlayer vs PlaySound

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello there,

I am programming a simple game for Windows Mobile, managed code C# with .NET
compact framework. In the game, I want to play some sounds (which I've stored
as sound resources in my project) when some actions occur. I found two ways:
The Media.Soundplayer .NET class and the PlaySound API method. I tried both
ways and there is a problem:

The API call to PlaySound just works fine, doesn't have any glitch.
The SoundPlayer.Play method has a serious glitch. The game stucks for about
a second (or half a second) before the sound begins to play. Is there
something wrong with the SoundPlayer.Play method, or am I doing something
wrong?
 
Hi,

I assume you are using .NETCF v3.5 beta as prior versions don't have the
System.Media classes. The way this class works is it loads in the file
internally and plays from the local cache of data. PlaySound simply plays
directly from the given file path (or memory buffer). You can work around
this by calling the Load() or LoadAsync() methods elsewhere in your code
prior to needing the sound. If the load is already done Play should perform
much better. Currently Play is having to first load the file which explains
the delay.

Peter
 
Thanks Peter for your quick response,
& congrats for your MVP

Well, I had tried what you said, in two ways: loading the sound from a file,
and loading the sound from a MemoryStream. They presented glitches. But in
the end I found out what the real problem was: Not surprisingly, I was
running the program always through Visual Studio (which monitors the program
while debugging and imposes a certain load on it), and that was why the
SoundPlayer presented glitches. I ran the program by itself and everything
worked just fine. Thanks for your response.
I should comment that SoundPlayer was a very nice addition in v3.5.
 
Back
Top