Website Design United States, Website Design California, Website Designing United States, Website Designing California

theraje's VB6 Game Tutorial Series I

Playing Sound Effects

Well, hello and greetings to you, game programmer of the novice level! How are you today? I hope you're rip-roarin'-ready to learn how to play sound effects in your Visual Basic 6.0 game, because that's exactly what we're going to do in this tutorial, by using the Windows API function "sndPlaySound!"

As we always do when using Windows API calls from Visual Basic, we need to get our API Text Viewer out and load the WIN32API file. Search for sndPlaySound, and copy it, declaring it Private (to a form) or Public (in a module) in the General section. The sndPlaySound function is now at our disposal! Here's the declaration:

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Once we have that, we need to make three constants - one that tells sndPlaySound to play asynchronously, another to tell it to loop a sound, and a third to make sure the sound doesn't interrupt another.

Private Const sndAsync = &H1
Private Const sndLoop = &H8
Private Const sndNoStop = &H10

There are a number of other constants pertaining to sndPlaySound, but these are the only ones we really need. The sndAsync constant tells sndPlaySound to play a sound asynchronously - in other words, it won't interrupt program flow. On the other hand, playing a sound synchronously brings the program to a screeching halt until the sound is finished - not something we usually want in a game. If you do want to play a sound synchronously, just add a new constant with a value of &HO. The sndLoop constant simply tells sndPlaySound to keep playing a sound until we tell it to stop. sndNoStop tells sndPlaySound not to play a sound if another is already playing.

And, since WAV files are usually stored outside of your program, I'll throw in a bonus - the GetAppPath function! What this does is it finds the directory the program is running from, adds a backslash if necessary (since some computers add it, some don't, and if you don't get it right, your program will throw an error at you), and allows you to add filenames to the end so you can reference them more easily. Here it is:

Private Function GetAppPath()
GetAppPath = App.Path
If Right$(GetAppPath, 1) <> "\" Then
GetAppPath = GetAppPath & "\"
End If
End Function

With that, we can store our sound effects in the same directory as our game file, and easily be able to reference them by creating a string to store the value of GetAppPath (we'll call it GamePath), adding to it a string with our file name (say, sound1.wav), and using that string to find our file, like so:

GamePath = GetAppPath
sndPlaySound GamePath & "sound1.wav", sndAsync

Now that we have a way to find our sound files, we can go about playing them. Create a command button and name it cmdPlay. Add the following snippet to its Click event:

sndPlaySound GamePath & "sound1.wav", sndAsync

Save it, and run the program. Click cmdPlay and see what happens. Assuming you actually have a WAV file in your program's directory named "sound1.wav," you should hear your sound play. If that's not the problem, make sure you declare a string named GamePath in the general declarations area, as well as set GamePath to the return value of the GetAppPath function (GamePath = GetAppPath).

Simple, eh? Just give sndPlaySound the file to look for and the sndAsync constant as an argument, and you're playing sound. Groove to it, baby! Now let's make another button, called cmdLoop, and place the following code in its Click event:

sndPlaySound GamePath & "sound1.wav", sndAsync Or sndLoop

Now, save the project, and try out the new button. It just keeps going, and going, and... well, you get the point. That's what adding the sndLoop constant does. To stop the sounds, simply close the form. Ooh, didn't work, did it? Ha ha! You have to tell it to stop, silly person. How? Before you run around screaming, "make it stop," just put on one more button, named cmdStop, and enter the following code... where? Why, in the Click event, of course!

sndPlaySound vbNullString, sndAsync

That's going to stop all sounds from playing. One of the drawbacks of sndPlaySound is that there is, to my knowledge, no way to play more than one sound at the same time. Unless you're running a simple game, you might just want to try DirectSound, part of DirectX, instead.

This concludes another game programming tutorial. If you want the source for this tutorial, as well as a sound clip, click here. Now, go get programming! Until next time... adios, amigos!

Author Information:

Clint V Franklin


Add your comments here.



You can also send feedback to

Solteykr - January 10, 2005 5:21 AM

Great! thanks! :)

KokiriChild - January 26, 2005 6:00 PM

Ahh! All of your tutorials have been fantastic, and since reading your tutorials I've developed a very small (and limited) 2D RPG engine in VB!

Many thanks!

Debadeep - January 28, 2005 8:05 AM

Wonderful! I didn't get these in any text book.

Ryan - May 15, 2005 12:08 PM

this has helped so much, i will be able to use this so much in our high school's prog 2 project, we just learned collision detection and the applications of this will we fun to work with and fun to play

generalls zero hour - July 5, 2005 8:30 AM

GamePath = GetAppPath
sndPlaySound GamePath & "sound1.wav", sndAsync

hofa - November 18, 2005 3:44 AM

will it be possible to play file like .mp3, wma...

martin hardy - December 3, 2005 8:47 AM

Great stuff!! Will this code bypass the windows mute and volume control?? What Iwant to do is from vb6 turn on and off mute. Any sugestions??



© 2008-2009