Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this code play a sound clip?!

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
0
0
NZ
Hi Folks

I thought I had figured this out but obviously not! I simply want my C# program to play a specified sound clip and perform a system beep. The beep works great but I just can't get the wav file to play. Here's the code:

using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class Win32 {
[DllImport("user32.dll")]
public static extern bool MessageBeep(uint soundtype);
}

public class Win32Beep
{
public static void Main()
{
string windir = "C:\\Media";
string soundfile = windir + "\\sound.wav";
Win32.MessageBeep(0);
File.Open(soundfile);
}
}

Any idea what I'm doing wrong? Obviously the File.Open is being used incorrectly but I'm stuck as to what to try instead.

Cheers,
Kenny.
 
I am a little confused. I do not see where you are actully even trying to play the sound? You are just opening a file. You are not doing anything with it.

I am not sure even if C#/.NET has native sound support yet. The only way I can think of doing what you want is using the Windowns Multi-Media DLL (WinMM.DLL) PlaySound function.
Code:
[DllImport("WinMM.DLL")]
private static extern long PlaySound(string lpszName, long hModule, long dwFlags);
Then instead of File.Open(soundfile), just use the above function like so:
Code:
PlaySound(soundfile, 0, 0);
That should do it. I have not tried this, but I do not see why it shouldn't work. Also I recommend that you make the extern COM functions private to a single class, and provide either C# methods to access the functionality, or just keep it's use hidden in the class or classes that needs them.

I hope this helps...

Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top