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.