Click here to Skip to main content
6,596,602 members and growing! (20,207 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate

Playing wav files with .NET

By Edward Moemeka

This article shows simple straightforward way to play wav files in .NET using c#
C#.NET 1.0, .NET 1.1, WinXPVS.NET2003, Dev
Posted:26 Jan 2004
Views:26,947
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 2.46 Rating: 1.93 out of 5
12 votes, 63.2%
1
1 vote, 5.3%
2

3

4
6 votes, 31.6%
5

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 typing 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Edward Moemeka


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
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jan 2004
Editor:
Copyright 2004 by Edward Moemeka
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project