Playing Wavs with sndPlaySound




First of all comes the lesson in the API:

From Mayuresh Online

API (Application Programers Interface) is a set of predefined Windows functions used to control the appearance and behaviour of every Windows element (from the outlook of the desktop window to the allocation of memory for a new process). Every user action causes the execution of several or more API function telling Windows what's happened.

It is something like the native code of Windows. Other languages just act as a shell to provide an automated and easier way to access APIs. VB has done a lot in this direction. It has completely hidden the APIs and provided a quite different approach for programming under Windows.

Here is the place to say that, Every line of code you write in VB is beening translated by VB into API function and sent to Windows. Thus calling something like Form1.Print ... causes VB to call TextOut API function with the needed parameters (either given by you in the code, or taken by some defaults).

Also, when the user click a button on your form, Windows sends a message to your windows procedure (that is eventually hidden for you), VB gets the call, analyses it and raises a given event (Button_Click) for you.

The API functions reside in DLLs (like User32.dll, GDI32.dll, Shell32.dll, ...) in the Windows system directory.

In this lesson you'll learn how to use the Windows API sndPlaySound.  First of all, you need to declare it.  This is the declaration:

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

It needs to be Public if you declare it in a module, if you do it at the top of the form that uses it, you don't need to have the word public.  What this means is...it's creating a function, just like what happens when you do:

Public Function sndPlaySound(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
End Function

Except, this isn't a function you write, it's one that you get from a .dll file (dynamic link library) in the windows system folder.  the file is "winmm.dll".  That really is unimportant to you, you just need to know how to declare it.  What IS important though is the two paramaters it creates:

lpsxSoundName = a string containing the path (or relative path) to the file you wish to play.  If you want to do a relative path, use [ App.path & "\blahblahblah.wav" ]  where App.Path is the folder your program is running in, and this will work as long as the program is running in the same folder as the wav file you are trying to play.

uFlags is a little more complicated...it is basically the options for playing the sound, so along with the function above, you'll want to add some constants:

Public Const SND_ASYNC = &H1
' The sound is played asynchronously and the function returns immediately after beginning the sound. To terminate an asynchronously played sound, call sndPlaySound with lpszSoundName set to NULL.
Public Const SND_LOOP = &H8 
' The sound plays repeatedly until sndPlaySound is called again with the lpszSoundName parameter set to NULL. You must also specify the SND_ASYNC flag to loop sounds.
Public Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file, not really something useful to you.
Public Const SND_NODEFAULT = &H2 
' If the sound cannot be found, the function returns silently without playing the default sound.
Public Const SND_NOSTOP = &H10 
' If a sound is currently playing, the function immediately returns FALSE, without playing the requested sound.
Public Const SND_SYNC = &H0 
'The sound is played synchronously and the function does not return until the sound ends.  This is default.

Public same as above.

Now, a few things that may be useful to know.  SND_ASYNC will play a sound and stop the sound that was being played if there was one being played by sndPlaySound.  SND_SYNC (default) doesn't allow anything else to happen until the sound ends (your program will pause til the sound ends). Those are the two most useful, that and SND_LOOP, but that's self-explanatory I think.

Now, you have a function, and you have parameters, so you need to call the function just like you would any other, we're going to use SND_ASYNC because I think that's most useful, and we're going to assume there is a file "ring.wav" in the folder the program is running from:

Public Sub Command1_Click()
Dim ReturnVal As Long
ReturnVal = sndPlaySound(App.path & "\ring.wav", SND_ASYNC)
End Sub


It really is that simple, using that code, and having that file in the correct location, will play the sound "ring.wav" and since sounds are such an important part of a game, this should be very helpful.

Any other questions Email me.


* Home * VB Programming Guides
 ~

Copyright 2003 Chris Mason All rights reserved.
Contact me
setstats 1