This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

C #(Csharp) code to how to play a wave file (.wav) using PlaySound() from the winmm.dll

See how to play a wave file (.wav) using PlaySound() from the winmm.dll


SOURCE CODE:
// play a wave file using PlaySound() from the winmm.dll
// a Windows Application

using System;
using System.Runtime.InteropServices; // DllImport()
using System.Drawing;
using System.Windows.Forms;

namespace PlayWave1?
{

public class WAVSounds
{
DllImport("WinMM.dll")
public static extern bool PlaySound(byte[]wfname, int fuSound);

// possible values for SoundFlags argument in PlaySound from mmsystem.h
public int SND_SYNC = 0x0000; // play synchronously (default)
public int SND_ASYNC = 0x0001; // play asynchronously
public int SND_NODEFAULT = 0x0002; // silence (!default) if sound not found
public int SND_MEMORY = 0x0004; // pszSound points to a memory file
public int SND_LOOP = 0x0008; // loop the sound until next PlaySound
public int SND_NOSTOP = 0x0010; // don't stop any currently playing sound
public int SND_NOWAIT = 0x00002000; // don't wait if the driver is busy
public int SND_ALIAS = 0x00010000; // name is a Registry alias
public int SND_ALIAS_ID = 0x00110000; // alias is a predefined ID
public int SND_FILENAME = 0x00020000; // name is file name
public int SND_RESOURCE = 0x00040004; // name is resource name or atom
public int SND_PURGE = 0x0040; // purge non-static events for task
public int SND_APPLICATION = 0x0080; // look for application-specific association

public void Play(string wfname,int SoundFlags)
{
byte[] bname = new Byte256; //Max path length
bname = System.Text.Encoding.ASCII.GetBytes(wfname);
PlaySound(bname,SoundFlags);
}

public void StopPlay()
{
PlaySound(null,SND_PURGE);
}
} //End WAVSounds class


// 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";
}

STAThread
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)
{
WAVSounds ws = new WAVSounds();
ws.Play(openFileDialog1.FileName,ws.SND_ASYNC);
}

void BtnStopClick(object sender, System.EventArgs e)
{
WAVSounds ws = new WAVSounds();
ws.StopPlay();
}
}
}

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger