Playing an audio file

  • Thread starter Thread starter Greg Smith
  • Start date Start date
G

Greg Smith

Hi, is there a simple way to play a short audio file? Some of my users
would like to hear "There is a new order for your approval" when ever the
situation calls for it. Is this do-able?

Any help is greatly appreciated.
 
This is easily doable if you use the windows API

First put this into the using section:
using System.Runtime.InteropServices;

Then define the funciton from the API:
[DllImport("winmm.dll")]
private static extern bool PlaySound( string lpszName, int
hModule, int dwFlags );

then provide a wrapper method for the API function:
private void playBtn_Click(object sender, System.EventArgs e)
{
String fileName = this.soundBox.Text;
PlaySound( fileName, 0, 1 );
}

The parameters of the windows API call are as follows:
lpszName - A string that specifies the sound to play.
hModule - Handle to the executable file that contains the
resource to be loaded. We pass in a NULL value instead.
dwFlags - Flags for playing the sound. We pass in SND_ASYNC.
The sound is played asynchronously and PlaySound returns immediately
after beginning the sound.

You can check out all this information here:
http://www.publicjoe.f9.co.uk/csharp/csharp17.html

Hope it helped,
Phil
 
Back
Top