TIP: Playing a WAV File with C# 2.0

Playing a WAV file in C# can be exceedingly simple, thanks to the addition of the SoundPlayer within the .NET Framework. The SoundPlayer class is available in the System.Media namespace.

To play a WAV file, you only need to create a SoundPlayer, add the location of the WAV file, and then play the file. This takes all of three lines of code:

System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
myPlayer.SoundLocation = @"c:\click.wav";

With these three lines of code, the click.wav file would be loaded and played. If you’d like the sound to loop, you can use the PlayLooping() method instead of the Play() method.

With the ease with which WAV files can be played, you can see that you can quickly add sound effects to your applications. At the first click of a button, you can have a nose occur, or when a user types in text. You simply need to privide WAV files and capture the appropriate events.

I’ve included a super simple example with a rather crude WAV file sound so you can get the idea. This example has three buttons. The first simply plays a WAV file. The second plays a WAV file in a continuous loop (until you click another button or end the program). The third button plays a WAV file before doing something. In this case, the something is simply updating the time on the form. The code for this is below and requires the two WAV files called simple.wav and click.wav; they are included in the download file.

As you can see, this is not rocket science. This tip, however, barely scratches the surface of what you can do with WAV files and the Media classes.

The PlayWav program. (Note that most of this code is Windows Forms code generated by the IDE):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WavPlayer
   public class Form1 : Form
      /// <summary>
      /// Required method for Designer support -- do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
         this.button1 = new System.Windows.Forms.Button();
         this.button2 = new System.Windows.Forms.Button();
         this.button3 = new System.Windows.Forms.Button();
         this.label1  = new System.Windows.Forms.Label();
         // button1
         this.button1.Location = new System.Drawing.Point(39, 25);
         this.button1.Name = "button1";
         this.button1.Size = new System.Drawing.Size(221, 46);
         this.button1.TabIndex = 0;
         this.button1.Text = "Simply Play WAV";
         this.button1.UseVisualStyleBackColor = true;
         this.button1.Click +=
            new System.EventHandler(this.button1_Click);
         // button2
         this.button2.Location = new System.Drawing.Point(39, 89);
         this.button2.Name = "button2";
         this.button2.Size = new System.Drawing.Size(221, 46);
         this.button2.TabIndex = 1;
         this.button2.Text = "Loop WAV";
         this.button2.UseVisualStyleBackColor = true;
         this.button2.Click +=
            new System.EventHandler(this.button2_Click);
         // button3
         this.button3.Location = new System.Drawing.Point(39, 153);
         this.button3.Name = "button3";
         this.button3.Size = new System.Drawing.Size(221, 46);
         this.button3.TabIndex = 2;
         this.button3.Text = "Click Sound";
         this.button3.UseVisualStyleBackColor = true;
         this.button3.Click +=
            new System.EventHandler(this.button3_Click);
         // label1
         this.label1.AutoSize = true;
         this.label1.Location = new System.Drawing.Point(36, 212);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(35, 13);
         this.label1.TabIndex = 3;
         this.label1.Text = "label1";
         // Form1
         this.AutoScaleDimensions =
            new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode =
         this.ClientSize = new System.Drawing.Size(292, 266);
         this.Name = "Form1";
         this.Text = "Form1";

      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;
      private System.Windows.Forms.Button button3;
      private System.Windows.Forms.Label label1;

      //***** Real code starts here ****

      private System.Media.SoundPlayer myPlayer;

      public Form1()
         // create a player to be used in the application
         myPlayer = new System.Media.SoundPlayer();

      private void button1_Click(object sender, EventArgs e)
         myPlayer.SoundLocation = @"c:\simple.wav";

      private void button2_Click(object sender, EventArgs e)
         myPlayer.SoundLocation = @"c:\simple.wav";

      private void button3_Click(object sender, EventArgs e)
         myPlayer.SoundLocation = @"c:\click.wav";
         label1.Text = "Doing something...." +

      static void Main()
         Application.Run(new Form1());

More by Author

Must Read