Playing WAV Sound Files in Visual Basic
Have you ever wanted to play a WAV file from your Visual Basic applicaiton? Here's the API call you need to use to play WAV files:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias sndPlaySoundA"_
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
All you have to do to play a WAV file now is tell the function what file you'd like to play (or in computer speak: ByVal lpszSoundName As String) and how you'd like to play it (ByVal uFlags As Long).
First things first. I'd like to play a WAV 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 WAV 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
To play my WAV file in a loop, all I have to do is declare the SND_LOOP constant in my declarations section and then pass SND_LOOP as my uFlags argument. So, the final function call would look something like this:
sndPlaySound App.path & "\ding.wav", SND_LOOP
A loop isn't the only method for playing a WAV file, however. As you can see, there are more constants yet unexplained:
SND_ASYNC
Allows us to play WAVs with the ability to interrupt currently playing WAVs. By that I mean that we can play our WAV at any time and ensure that it will be heard.
SND_NODEFAULT
Ensures that if the WAV 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 WAV file, but does not return control to the program until the WAV 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 WAV file is already playing, our WAV file will not interrupt it. Use this if you want to ensure that your WAVs are never cut short.
SND_MEMORY
Plays a WAV file that is already stored in memory. The routine to store a WAV file in memory will not be shown here, but it simply involves storing the WAV 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 WAV 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
That's all there really is to know about playing WAV files using the windows API. To download sample source code using the principles discussed here click here.
About this Tutorial
This tutorial is from The Game Progamming Wiki which is published under the GNU Free Documentation License 1.2.

0 comments:
Post a Comment