Adding sound to a game?

lompa

Member
Joined
Feb 17, 2004
Messages
6
i have some code and some .wav files in the folder that contains my sourse code, but instead of playing the defined sound, eg when a bullet it fired, it plays a windows sound, this is the code i have any help much appreciated

private Thread oThread = null;
[DllImport("winmm.dll")]
public static extern long PlaySound(String lpszName, long hModule, long dwFlags);

private string m_strCurrentSoundFile;
public void PlayASound()
{
if (m_strCurrentSoundFile.Length > 0)
{
PlaySound(Application.StartupPath + "\\" + m_strCurrentSoundFile, 0, 0);
}
//m_strCurrentSoundFile = "";
//m_nCurrentPriority = 3;
//oThread.Abort();
}

int m_nCurrentPriority = 3;
public void PlaySoundInThread(string wavefile, int priority)
{
if (priority <= m_nCurrentPriority)
{
//m_nCurrentPriority = priority;
//if (oThread != null)
//oThread.Abort();

m_strCurrentSoundFile = wavefile;
oThread = new Thread(new ThreadStart(PlayASound));
oThread.Start();

}

}



Then under the keydown event for the fire bullet i have

PlaySoundInThread("laz.wav", 3);

any help is much appreciated, p.s i have commented out some of the code above
 
well you need to specify SND_FILENAME in the flags to play a file using its path and int rather than long , heres a quick example i knocked up for you ...
C#:
		public string filePath = string.Empty;

		private void button1_Click(object sender, System.EventArgs e)
		{
			filePath = @"C:\Program Files\Messenger\newalert.wav";
			System.Threading.Thread wavThread = new System.Threading.Thread(new System.Threading.ThreadStart(WavProc));
			wavThread.Start();
		}

		static void WavProc()
		{
			Form1 frm = (Form1) Form.ActiveForm; /// to allow access to the string " filePath " s value.
			_win32.PlaySound(frm.filePath , 0 , _win32.SND_FILENAME);
		}
	}
}

public class _win32
{
	[System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint="PlaySoundA")]
	public static extern int PlaySound (string lpszName, int hModule, int dwFlags);

	public const int SND_FILENAME = 0x20000; /// specify its going to use a file path to open the .Wav

}
 
Back
Top