Software Development

Play WAV files from a VB program to improve user interaction

Signal application events for users using Microsoft Windows Multimedia functions from a Visual Basic 6 program.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

You can improve a program's user interface by using short sounds to signal certain events such as the completion of a download or the selection of a command. With the right Windows Multimedia function, playing WAV files from a VB program is child's play. However, the system must be equipped with a sound card for this to work.

The API function you need is called PlaySound. Here's its declaration:

Public Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVallpszName As String, _
  ByValhModule As Long, ByValdwFlags As Long) _
  As Long

The first argument is the name, including the path, of the Wave file to play. The second argument isn't used when playing sound files, and you should pass a value of zero. (This function can also play sounds that are associated with system events, but that topic isn't covered here.) The final argument consists of flags that control various aspects of how the function works.

For present purposes, two flags are used. They are (with the constants that are typically used for them):

SND_ASYNC (value= 1): play asynchronously, which means that the function returns while the sound is still playing.
SND_FILENAME (value = &H20000): the first argument is a filename.

Thus, the following code plays the sound DingDong.wav:

PlaySound "dingdong.wav", CLng(0), _
   SND_ASYNC Or SND_FILENAME

When playing sound files in a program, there are two other considerations. First, you want to make sure that the specified wave file exists or else an error will occur. This can be done with this simple function:

Public Function FileExists(FullFileName) As Boolean

' Passed a filename (with path) returns
' True if the file exists, False if not.

Dim s

s = dir(FullFileName)

If s = "" Then
    FileExists = False
Else
    FileExists = True
End If

End Function

Second, the program should have an option setting that lets users turn the sound off. This can be maintained as a global Boolean variable with a name such as gProgramSoundsEnabled.

My approach is to deal with both of these considerations in a function as follows. (This code assumes that the wave files are stored in the application folder.)

Private Sub PlaySoundX(filename As String)

' If sound is enabled and filename exists,
' play the specified sound.

filename = App.path & "\" & filename

If FileExists(filename) And gProgramSoundsEnabled Then
    PlaySound filename, CLng(0), SND_ASYNC Or SND_FILENAME
End If

End Sub

With this function in place, the program can play any wave file like this:

PlaySoundX "DingDong.wav"

Another way of playing sounds from a VB program involves including a multimedia control on a form. When you don't need the extra features of the control, the technique presented here is a lot easier.

Peter Aitken has been programming with Visual Basic since Version 1.0. He has written numerous books and magazine articles on Visual Basic and other computer and programming topics.

0 comments

Editor's Picks