Click here to Skip to main content
Licence CPOL
First Posted 26 Jan 2004
Views 30,775
Downloads 97
Bookmarked 17 times

Playing Wav files with .NET

By | 26 Jan 2004 | Article
This article shows a simple and straightforward way to play Wav files in .NET using C#.

Introduction

This article illustrates how simple it is to add sound functionality to any .NET application. I use the PlaySound Win32 function located in the winmm.dll library. The sample does not include all the different flags associated with fdwSound; these can be acquired in a number of different ways. Try opening a Managed C++ project and type the PlaySound function in there. It should give you the signature. For any of the fdwSound constants, you can type them in, and either hover the mouse over to get the value, or right click and select Go to definition to see where they are defined.

Here is the basic code for it:

//first I define the function
[DllImport("winmm.dll")]
static extern bool PlaySound(string file, int module, int flags );

//then in the click event of the form i specify this
OpenFileDialog media = new OpenFileDialog();
media.RestoreDirectory = true;
if(media.ShowDialog() == DialogResult.OK){
     string waveFile = media.FileName;
     string extension = System.IO.Path.GetExtension(waveFile);
     if(extension.ToLower() == ".wav"){
          PlaySound(waveFile,0,0x0008); //yep it's really this simple.
    }
}

You could just use the function with zeros for every other parameter:

PlaySound(somefile.wav",0,0);

That's it, you're good to go.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Edward Moemeka



United States United States

Member

Hi I'm Edward Moemeka,
For more interesting articles about stuff check out my blog at http://moemeka.blogspot.com
To correspond, email me at edward.moemeka@synertry.com
To support my company, thus help me feed my family, check out our awesome online preview at www.synertry.com. Remember, its in alpha Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 27 Jan 2004
Article Copyright 2004 by Edward Moemeka
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid