Richard Crist Posted December 17, 2004 Posted December 17, 2004 Senior Developer / .NET & C# newbie here. I just wanna beep when my "Time's Up" messagebox pops up. I've searched here and there and discovered the Metafile class PlaySound method. It won't let me use it because of protections and so forth. How can I beep? I reckon there are DirectX SDK's and so forth, but for the record is there a simple way to beep in .NET? :confused: Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
Diesel Posted December 17, 2004 Posted December 17, 2004 [DllImport("kernel32.dll")] private extern static bool Beep ( int dwFreq , int dwDuration ) ; public static void BeepPcInternal(int freq , int duration) { Beep(freq,duration); } Quote
Richard Crist Posted December 17, 2004 Author Posted December 17, 2004 Thank you, Diesel! I will check this out and let you know how it goes. :) Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
Richard Crist Posted December 17, 2004 Author Posted December 17, 2004 It Worked! [DllImport("kernel32.dll")] private extern static bool Beep ( int dwFreq , int dwDuration ) ; public static void BeepPcInternal(int freq , int duration) { Beep(freq,duration); } I was able to get it to work! At the top of my C# source I had to include a using System.Runtime.InteropServices; but after I did that it worked! It only beeps the pc speaker. I reckon you have to do fancy SDK stuff to use the sound card, but I am happy to have my beep. Thank you very much! :) Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
Mike_R Posted December 20, 2004 Posted December 20, 2004 Actually, you could just use the PlaySound API. There may be built-in .Net methods as well (I don't know) but PlaySound works fine: Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _ (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long Private Const SND_ASYNC As Integer = &H1 Private Const SND_ALIAS As Integer = &H10000 Then call:Call PlaySound("C:\\Windows\\Media\\Tada.wav", 0, SND_ASYNC Or SND_ALIAS) :), Mike Quote Posting Guidelines Avatar by Lebb
Richard Crist Posted December 20, 2004 Author Posted December 20, 2004 PlaySound Also Worked! Actually, you could just use the PlaySound API. There may be built-in .Net methods as well (I don't know) but PlaySound works fine: Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _ (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long Private Const SND_ASYNC As Integer = &H1 Private Const SND_ALIAS As Integer = &H10000 Then call:Call PlaySound("C:\\Windows\\Media\\Tada.wav", 0, SND_ASYNC Or SND_ALIAS) :), Mike Using your code above as a starting point I was able to use PlaySound. :) The former Beep routine beeps the pc internal speaker with the ability to control frequency and duration. Using Beep you can do cool things with several Beeps in a row with different frequencies and durations. The PlaySound routine uses the pc's sound card. The example you gave (Tada.wav) gives the familiar ding sound. I converted the code above to C#. I looked around and found the definitions for the other SND_ parameters. And, as with the Beep, to use the DllImport option I had to include "using System.Runtime.InteropServices;" at the top of my source. My converted code with complete SND_ definitions is shown below. Also, the original PlaySound I tried didn't do the SND_LOOP option. I found the PlaySoundA routine on the web and it does the SND_LOOP option. Thank you so much to both of you, because I was getting no where without your help! :) // constants for use with PlaySound public const int SND_ALIAS = 0x10000; //lpszName is a string identifying the name of the system event sound to play. public const int SND_ALIAS_ID = 0x110000; //lpszName is a string identifying the name of the predefined sound identifier to play. public const int SND_APPLICATION = 0x80; //lpszName is a string identifying the application-specific event association sound to play. public const int SND_ASYNC = 0x1; //Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background. public const int SND_FILENAME = 0x20000; //lpszName is a string identifying the filename of the .wav file to play. public const int SND_LOOP = 0x8; //Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified. public const int SND_MEMORY = 0x4; //lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM. public const int SND_NODEFAULT = 0x2; //If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success. public const int SND_NOSTOP = 0x10; //If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead. public const int SND_NOWAIT = 0x2000; //If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure. public const int SND_PURGE = 0x40; //Stop playback of any waveform sound. lpszName must be an empty string. public const int SND_RESOURCE = 0x4004; //lpszName is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle. public const int SND_SYNC = 0x0; //Play the sound synchronously -- do not return until the sound has finished playing. //[DllImport("winmm.dll")] // first usage //private extern static long PlaySound (string lpszName, long hModule, long dwFlags); // first usage [DllImport("winmm.dll", EntryPoint="PlaySound")] // found on web - works with SND_LOOP public static extern int PlaySoundA(string lpszName, int hModule, int dwFlags); // found on web - works with SND_LOOP private void button1_Click(object sender, System.EventArgs e) { //PlaySound("C:\\Windows\\Media\\Tada.wav", 0, SND_ASYNC | SND_ALIAS); // first usage PlaySoundA("C:\\Windows\\Media\\Tada.wav", 0, SND_ASYNC | SND_ALIAS | SND_LOOP); // PlaySoundA for SND_LOOP ability System.Threading.Thread.Sleep(5000); PlaySoundA("C:\\Windows\\Media\\Tada.wav", 0, 0); } Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.