So there you are, looking at a program you've made, thinking to yourself "Man I wish I could add a poignant little 'ding' when the user pushes this button!" Well my friend, won't you play with my ding-a-ling?
Shut up Lucky.
Ok ok, here's the API call you need to use to play wave files:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
First things first. I'd like to play a wave file in my windows\media directory called "ding.wav", so for the lpszSoundName argument I pass "c:\windows\media\ding.wav". It's as simple as that! If the ding.wav were in the same directory as my program, however, I would pass App.path & "\ding.wav" since this will work no matter where my program is, so long as ding.wav is in the same place. ("App.path" returns a string containing the path to the directory in which the application is running.)
Second things second. I'd like to play my wave file in a loop, how do I communicate that to the function? Well, most API calls come equipped with a few constants, and this function is no exception:
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_NODEFAULT = &H2
Const SND_SYNC = &H0
Const SND_NOSTOP = &H10
Const SND_MEMORY = &H4
sndPlaySound App.path & "\ding.wav", SND_LOOP
A loop isn't the only method for playing a wave file, however (thank God!). As you can see, there are more constants yet unexplained:
SND_ASYNC allows us to play waves with the ability to interrupt currently playing waves. By that I mean that we can play our wave at any time and ensure that it will be heard.
SND_NODEFAULT ensures that if the wave file specified cannot be found then no other sound will be played in it's place (like some default windows noise!).
SND_SYNC plays a wave file, but does not return control to the program until the wave file has finished playing. This is very annoying if all we were interested in was playing a sound in the background of our program.
SND_NOSTOP ensures that if a wave file is already playing, our wave file will not interrupt it. Use this if you want to ensure that your waves are never cut short.
SND_MEMORY plays a wave file that is already stored in memory. The routine to store a wave file in memory will not be shown here, but it simply involves storing the wave in a string using a "Get" command (be sure to size the string properly first) and passing that string as the argument.
If we wish to use more than one of these constants in conjunction, we link them with an "or" statement so that both constants will have an effect. For example, it is best to use SND_ASYNC along with SND_LOOP to ensure that the program can continue functioning while the looped wave file is playing. If we used SND_SYNC with SND_LOOP, we would never get control back to our program! Example:
sndPlaySound App.path & "\ding.wav", SND_ASYNC or SND_LOOP
To download an example function using the principles discussed here, check out the Files Page or click here to download now.