VB icon

Play sound in C# without DirectX

Email
Submitted on: 1/1/2015 5:21:00 PM
By: Jon Davis (from psc cd)  
Level: Intermediate
User Rating: By 16 Users
Compatibility: C#
Views: 1912
 
     Why load DirectX to play a sound when you can just call the Windows library?
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Play sound in C# without DirectX
// Description:Why load DirectX to play a sound when you can just call the Windows library?
// By: Jon Davis (from psc cd)
//**************************************

// NOTE: Add these two lines at the top of the file:
//
//using System.Runtime.InteropServices;
//using System.Windows.Forms;
///<summary>
///Plays a sound from a byte array. 
///Note: If distortion or corruption of audio playback occurs, 
///try using synchronous playback, or save to a temp file and
///use the file-based option.
///</summary>
public static int PlaySound(byte[] audio, bool bSynchronous, bool bIgnoreErrors) {
	return PlaySound(audio, bSynchronous, bIgnoreErrors, false, false, false);
}
///<summary>
///Plays a sound from a byte array. 
///Note: If distortion or corruption of audio playback occurs, 
///try using synchronous playback, or save to a temp file and
///use the file-based option.
///</summary>
public static int PlaySound(byte[] audio, bool bSynchronous, bool bIgnoreErrors,
	bool bNoDefault, bool bLoop, bool bNoStop) {
	const int SND_ASYNC = 1;
	const int SND_NODEFAULT = 2;
	const int SND_MEMORY = 4;
	const int SND_LOOP = 8;
	const int SND_NOSTOP = 16;
	int Snd_Options = SND_MEMORY;
	if (!bSynchronous) {
		Snd_Options += SND_ASYNC;
	}
	if (bNoDefault) Snd_Options += SND_NODEFAULT;
	if (bLoop) Snd_Options += SND_LOOP;
	if (bNoStop) Snd_Options += SND_NOSTOP;
	try {
		return PlaySound(audio, 0, Snd_Options);
	} catch (Exception ex) {
		if (!bIgnoreErrors) {
			throw ex;
		} else {
			return 0;
		}
	}
}
public static int PlaySound(string sSoundFile, bool bSynchronous, bool bIgnoreErrors) {
	return PlaySound(sSoundFile, bSynchronous, bIgnoreErrors, false, false, false);
}
public static int PlaySound(string sSoundFile, bool bSynchronous, bool bIgnoreErrors,
	bool bNoDefault, bool bLoop, bool bNoStop) {
	const int SND_ASYNC = 1;
	const int SND_NODEFAULT = 2;
	const int SND_LOOP = 8;
	const int SND_NOSTOP = 16;
	if (!System.IO.File.Exists(sSoundFile)) {
		string WinDir = System.IO.Directory.GetParent(System.Environment.SystemDirectory).FullName;
		if (WinDir.Substring(WinDir.Length - 1, 1) != "\\") WinDir += "\\";
		if (System.IO.File.Exists(Application.StartupPath + "\\" + sSoundFile)) {
			sSoundFile = Application.StartupPath + "\\" + sSoundFile;
		} else if (System.IO.File.Exists(Application.StartupPath + "\\" + sSoundFile + ".wav")) {
			sSoundFile = Application.StartupPath + "\\" + sSoundFile + ".wav";
		} else if (System.IO.File.Exists(WinDir + "media\\" + sSoundFile)) {
			sSoundFile = WinDir + "media\\" + sSoundFile;
		} else if (System.IO.File.Exists(WinDir + "media\\" + sSoundFile + ".wav")) {
			sSoundFile = WinDir + "media\\" + sSoundFile + ".wav";
		} else if (!bIgnoreErrors) {
			throw new System.IO.FileNotFoundException("Sound file doesn't exist.");
		}
	}
	int Snd_Options = 0;
	if (!bSynchronous) {
		Snd_Options = SND_ASYNC;
	}
	if (bNoDefault) Snd_Options += SND_NODEFAULT;
	if (bLoop) Snd_Options += SND_LOOP;
	if (bNoStop) Snd_Options += SND_NOSTOP;
	try {
		return sndPlaySoundA(sSoundFile, Snd_Options);
	} catch (Exception ex) {
		if (!bIgnoreErrors) {
			throw ex;
		} else {
			return 0;
		}
	}
}
[DllImport("winmm.dll")]
private static extern int sndPlaySoundA(string lpszSoundName, int uFlags);
[DllImport("winmm.dll")]
private static extern int PlaySound(byte[] pszSound, Int16 hMod, long fdwSound);


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.