I implemented a class to play my wav files (as shown below) in my game (background music, effects such as explosions, fire, gun shots, etc...):
(class I found online when reading on how-to-play-sounds in C# with some slight changes)
So, right at the begining my goal is to load a WAV file (my background song) and have it repeat (loop) continuously in the backgroud ...
Then later on at some point in the application play an "explosion" sound (that lasts like 2 seconds) on top of the background without disrupting it...
So I am trying to accomplish this by doing the following:
As-is the BackGround music plays fine but the explosion sounds are never heard... I tried commenting out the background sounds and now the explosions work fine ... So it looks like it has a problem playing both at the same time (one on top of the other)...
But that is why I used the flag "WAVSounds.SND_NOSTOP", so that it wouldnt "stop any currently playing sound" (such as my background) but would still play (such as the explosion)...
What am I doing wrong? Am I using the right approach? Is there a better way to be addressing this type of situation?
Also - SIDE QUESTION - how do I control the sound properties? Specifically the VOLUME? (for example I want the Background music to be lower then the effects when they happen)...
How is this accomplished?
Any ideas, hints, and help would be greatly appreciated, thanks
Code:
public class WAVSounds
{
[DllImport("WinMM.dll")]
public static extern bool PlaySound(byte[]wfname, int fuSound);
// flag values for SoundFlags argument on PlaySound
public static int SND_SYNC = 0x0000; // play synchronously (default)
public static int SND_ASYNC = 0x0001; // play asynchronously
public static int SND_NODEFAULT = 0x0002; // silence (!default) if sound not found
public static int SND_MEMORY = 0x0004; // pszSound points to a memory file
public static int SND_LOOP = 0x0008; // loop the sound until next sndPlaySound
public static int SND_NOSTOP = 0x0010; // dont stop any currently playing sound
public static int SND_NOWAIT = 0x00002000; // dont wait if the driver is busy
public static int SND_ALIAS = 0x00010000; // name is a Registry alias
public static int SND_ALIAS_ID = 0x00110000; // alias is a predefined ID
public static int SND_FILENAME = 0x00020000; // name is file name
public static int SND_RESOURCE = 0x00040004; // name is resource name or atom
public static int SND_PURGE = 0x0040; // purge non-static events for task
public static int SND_APPLICATION = 0x0080; // look for application-specific association
//-----------------------------------------------------------------
public static void Play(string wfname, int SoundFlags)
{
byte[] bname = new Byte[256]; //Max path length
bname = System.Text.Encoding.ASCII.GetBytes(wfname);
PlaySound(bname, SoundFlags);
}
//-----------------------------------------------------------------
public static void StopPlay()
{
PlaySound(null, SND_PURGE);
}
}
So, right at the begining my goal is to load a WAV file (my background song) and have it repeat (loop) continuously in the backgroud ...
Then later on at some point in the application play an "explosion" sound (that lasts like 2 seconds) on top of the background without disrupting it...
So I am trying to accomplish this by doing the following:
Code:
// Start Level Music (Background)
WAVSounds.Play("background.wav", WAVSounds.SND_ASYNC + WAVSounds.SND_LOOP);
...
... much later on ...
...
// Play Explosion Sound
WAVSounds.Play("explosion.wav", WAVSounds.SND_ASYNC + WAVSounds.SND_NOSTOP);
As-is the BackGround music plays fine but the explosion sounds are never heard... I tried commenting out the background sounds and now the explosions work fine ... So it looks like it has a problem playing both at the same time (one on top of the other)...
But that is why I used the flag "WAVSounds.SND_NOSTOP", so that it wouldnt "stop any currently playing sound" (such as my background) but would still play (such as the explosion)...
What am I doing wrong? Am I using the right approach? Is there a better way to be addressing this type of situation?
Also - SIDE QUESTION - how do I control the sound properties? Specifically the VOLUME? (for example I want the Background music to be lower then the effects when they happen)...
How is this accomplished?
Any ideas, hints, and help would be greatly appreciated, thanks