Once in awhile you may want to play certain sounds for certain reasons in your applications. You may want more than the generic Beep() that is in Visual Basic and .NET. But if you ONLY want a simple Beep to sound through the speakers, then use the simple built-in Sub that is available with ALL versions of Visual Basic as seen below…
Update: Thanks to Andreas for mentioning that Win95/Win98/WinME will only play the default beep sound using the API call below. Custom Freq/Durations are not supported until Win NT/2000 and above including XP/Vista.
Update Again: Windows Vista 64-bit will only play the default beep with this API code. Probably all 64-bit versions. So it looks like this code has become obsolete. Sorry about that 🙁
' 'This Sub plays a simple beep sound through the computer. Supported in ALL versions of VB. Beep()
BUT, what if you wanted to make different beeps or tones that could mean different things using certain frequencys and/or different duration combinations? What if you wanted to make a Morse Code application?
The code in this article will show a way to beep in VB 6.0 and Visual Basic.NET and an extra method for Visual Basic 2008/2010, and newer. The way to do it with ALL versions of Visual Basic it to use a simple little Microsoft Windows API call. The API code is below…
Visual Basic 6.0 Declaration…
' 'This is the little API call to give us some say in the duration and frequency of the sound... Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Long, ByVal soundDuration As Long) As Long
Visual Basic.NET 02/03 and 05/08/2010
' 'This is the little API call to give us some say in the duration and frequency of the sound... Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Int32, ByVal soundDuration As Int32) As Int32
The soundFrequency parameter is where you can specify how high or low you want the frequency to be. I looks to have a Minimum supported value of 37hz and Maximum of 32767hz.
The soundDuration parameter is for how long you want the sound to play in milliseconds. IF you set the parameter to 5000, is will keep generating that tone for 5 seconds before it stops. Something to remember too is that this function will consume the calling thread. So if you set a tone for long periods of time your application will have to wait until the tone is finished generating before it will be responsive again if you call it inside your apps thread.
' 'This Function plays a simple beep sound through the computer at a frequency of 1000hz and for 3x seconds. Beep 1000, 3000
You can get the return value of the Function if you want. Especially if it doesn’t appear to be working. IF the Return Value is Zero(0) then the call failed. This could happen if the Frequency you set it to Low and/or to High. Nothing more I can really say…
The next method works with Visual Basic 2008/2010 and higher. Its actually a managed wrapper around the Beep() api function. Its located in the: System.Console Class.
This is the Sub Declaration:
You can simply call this Sub the same way you did the API version…
' 'This Sub uses the Beep() Subroutine under the Console Class and plays a simple beep sound through the computer at a frequency of 4000hz and for 1x second. Console.Beep(4000, 1000)
That’s all there is to it. If you want to make Morse code this code should be perfect for doing that. Hope you got something useful here. 🙂