Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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:
Posted

[DllImport("kernel32.dll")]

private extern static bool Beep ( int dwFreq , int dwDuration ) ;

 

public static void BeepPcInternal(int freq , int duration)

{

Beep(freq,duration);

 

 

}

Posted

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! :)

Posted

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

Posting Guidelines

 

Avatar by Lebb

Posted

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);
	}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...