• Beeps are nice, but many times you want to play more elaborate sounds.
Most sounds you hear played in Windows applications are saved in WAV files (files
with WAV extensions). These are the files formed when you record using one of
the many sound recorder programs available.
• WAV files are easily
played using DLL functions. There is more than one way to play such a file. We’ll
use the sndPlaySound function. This is a long function that requires two arguments,
a string argument with the name of the WAV file and a long argument indicating
how to play the sound. The usage syntax is:
Dim WavFile As String, SndType as Long,
RtnValue as Long
RtnValue = sndPlaysound(WavFile, SndType)
• SndType has many possible values. We’ll just look at two:
SND_SYNC - Sound is played to completion, then execution continues
SND_ASYNC - Execution continues as sound is played
Quick Example 7 - Playing WAV
Start a new application. Add a command button and a common dialog box. Copy
and paste the sndPlaySound Declare statement from the API Text Viewer program
into your application. Also copy the SND_SYNC and SND_ASYNC constants. When done
copying and making necessary scope modifications, you should have:
Private Declare Function sndPlaySound
Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName
As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_SYNC = &H0
Add this code to the Command1_Click procedure:
Private Sub Command1_Click()
Dim RtnVal As Integer
'Get name of .wav file to play
CommonDialog1.Filter = "Sound Files|*.wav"
RtnVal = sndPlaySound(CommonDialog1.filename, SND_SYNC)
Run the application. Find a WAV file and listen to the lovely results.