A small Windows Application to show you how to play a wave (.wav) sound file. I am using the C# IDE from SharpDevelop and the runtime dotnetfx 1.1 from Microsoft, both free downloads. This forms a small and fast student system to write and debug C# programs. From there you can later graduate to the omnipotent, drive space consuming, but more helpful Visual C# .NET.
Play a wave file (C#)
// play a wave file using PlaySound() from the winmm.dll
// a Windows Application tested with VCS.NET 2003 and SharpDevelop
using System;
using System.Runtime.InteropServices; // DllImport()
using System.Drawing;
using System.Windows.Forms;
namespace PlayWave1
{
// description of MainForm.
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnPlay;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button btnLoadFile;
public MainForm()
{
InitializeComponent();
openFileDialog1.Title = "Select a Wave Sound File";
openFileDialog1.Filter = "Wav Files(*.wav)|*.wav";
}
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
private void InitializeComponent()
{
this.btnLoadFile = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.btnStop = new System.Windows.Forms.Button();
this.btnPlay = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnLoadFile
//
this.btnLoadFile.Location = new System.Drawing.Point(24, 16);
this.btnLoadFile.Name = "btnLoadFile";
this.btnLoadFile.Size = new System.Drawing.Size(120, 23);
this.btnLoadFile.TabIndex = 0;
this.btnLoadFile.Text = "Load File";
this.btnLoadFile.Click += new System.EventHandler(this.BtnLoadFileClick);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(24, 112);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(128, 23);
this.btnStop.TabIndex = 2;
this.btnStop.Text = "Stop";
this.btnStop.Click += new System.EventHandler(this.BtnStopClick);
//
// btnPlay
//
this.btnPlay.Location = new System.Drawing.Point(24, 64);
this.btnPlay.Name = "btnPlay";
this.btnPlay.Size = new System.Drawing.Size(120, 23);
this.btnPlay.TabIndex = 1;
this.btnPlay.Text = "Play";
this.btnPlay.Click += new System.EventHandler(this.BtnPlayClick);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(304, 266);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnPlay);
this.Controls.Add(this.btnLoadFile);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
void BtnLoadFileClick(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
}
void BtnPlayClick(object sender, System.EventArgs e)
{
WSounds ws = new WSounds();
ws.Play(openFileDialog1.FileName, ws.SND_FILENAME|ws.SND_ASYNC);
}
void BtnStopClick(object sender, System.EventArgs e)
{
WSounds ws = new WSounds();
ws.StopPlay();
}
}
public class WSounds
{
[DllImport("WinMM.dll")]
public static extern bool PlaySound(string fname, int Mod, int flag);
// these are the SoundFlags we are using here, check mmsystem.h for more
public int SND_ASYNC = 0x0001; // play asynchronously
public int SND_FILENAME = 0x00020000; // use file name
public int SND_PURGE = 0x0040; // purge non-static events
public void Play(string fname, int SoundFlags)
{
PlaySound(fname, 0, SoundFlags);
}
public void StopPlay()
{
PlaySound(null, 0, SND_PURGE);
}
}
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
ashley dane 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.