How to Play a Waveform (.WAV) Sound File in Visual Basic

ID Number: Q86281

1.00

WINDOWS

Summary:

You can play a waveform (.WAV) sound file from Visual Basic by

calling the sndPlaySound API function from the MMSYSTEM.DLL file. In

order to be able to call the sndPlaySound API function, you must be

using either Windows version 3.1 or the Multimedia Extensions for

Windows version 3.0. The following information discusses the

sndPlaySound parameters, and includes an example of how to use this

function from Visual Basic.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

To use the sndPlaySound API from within a Visual Basic application,

you must Declare the sndPlaySound function in either the global

module or from within the Declarations section of your Code window.

Declare the function as follows:

Declare Function sndPlaySound Lib "MMSTSTEM.DLL" (ByVal lpszSoundName$

ByVal wFlags%) As Integer

Note: The above Declare statement must be written on just one line.

The parameters listed above are explained as follows:

Parameters

----------

lpszSoundName$

Specifies the name of the sound to play. The function first

searches the [sounds] section of the WIN.INI file for an

entry with the specified name, and plays the associated

waveform sound file. If no entry by this name exists, then it

assumes the specified name is the name of a waveform sound

file. If this parameter is NULL, any currently playing sound

is stopped.

wFlags%

Specifies options for playing the sound using one or more of

the following flags:

SND_SYNC

The sound is played synchronously and the function does

not return until the sound ends.

SND_ASYNC

The sound is played asynchronously and the function

returns immediately after beginning the sound.

SND_NODEFAULT

If the sound cannot be found, the function returns

silently without playing the default sound.

SND_LOOP

The sound will continue to play repeatedly until

sndPlaySound is called again with the lpszSoundName$

parameter set to null. You must also specify the

SND_ASYNC flag to loop sounds.

SND_NOSTOP

If a sound is currently playing, the function will

immediately return False without playing the requested

sound.

The sndPlaySound function returns True (-1) if the sound is played,

otherwise it returns False (0).

The following code example illustrates how to use the sndPlaySound

API function to play a waveform (.WAV) sound file.

Add the following code to the global module or general Declarations

section of your form:

Declare Function sndPlaySound Lib "MMSYSTEM.DLL" (ByVal

lpszSoundName$, ByVal wFlags%) As Integer

Global Const SND_SYNC = &H0000

Global Const SND_ASYN = &H0001

Global Const SND_NODEFAULT = &H0002

Global Const SND_LOOP = &H0008

Global Const SND_NOSTOP = &H0010

Add the following line of code to the appropriate function or

subroutine in your application:

SoundName$ = "c:\windows\tada.wav"

wFlags% = SND_ASYNC And SND_NODEFAULT

x% = sndPlaySound(SoundName$,wFlags%)

Note that if a large waveform (.WAV) sound file is specified and the

above call fails to play the file in its entirety, you will need to

adjust the settings on the appropriate sound driver.

For more information on adjusting the sound driver settings, query

on the following words in the Microsoft Knowledge Base:

Speaker and Sound and Driver and Settings and .Wav and File

Reference(s):

"Microsoft Multimedia Development Kit: Programmer's Reference"

version 1.0

Additional reference words: 1.00