IIM Home |
Well There are lots of pages with code similar to this, but I always thought that it looked really hokey, surely there had to be a better way to play a sound in .Net! I mean even VB5 had native methods for opening and playing wave files. Well I ran a C# decompiler against a piece of commercial software written in C# and low and behold was the same code, almost verbatim, as on all of the websites. I guess there isn't a better way after all. So here it is, again copied verbatim from the IIM sourcecode: using System.Runtime.InteropServices; using System.IO; namespace InternalInstantMessenger { /// <summary> /// I got this code from running a C# decompiler on a retail product /// Probably not best to admit that, but I figure it's pretty generic and /// I know that it works. well that, and it's the same as over a dozen web sites. /// </summary> public class SoundPlayer { [DllImport("Winmm.dll")] private static extern bool PlaySound(string Sound, int hMod, UInt32 dwFlags); public SoundPlayer() { } public static void Play(string filename) { // Play asynchronously // Asynchronously = 0x0001, // Don't wait if the driver is busy // NoWait = 0x00002000, // Name is file name // Filename = 0x00020000, if(File.Exists(filename)) { PlaySound(filename,0,0x0001|0x00020000|0x00002000); } } } |
|