HOWTO: Play a Waveform (.WAV) Sound File in Visual Basic

ID: Q86281


The information in this article applies to:

THE FOLLOWING SECTION APPLIES TO VISUAL BASIC VERSIONS 4.0 AND 5.0


SUMMARY

You can play a waveform (.wav) sound file from Microsoft Visual Basic for Windows 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 Microsoft Windows, version 3.1 or the Microsoft 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 for Windows.


MORE INFORMATION

To use the sndPlaySound API from within a Visual Basic for Windows application, you must declare the sndPlaySound function in the global module or in the Declarations section of the Code window. To declare the function, enter this Declare statement:


   Declare Function sndPlaySound Lib "WINMM.DLL" Alias "sndPlaySoundA" _
      (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long 

Here are explanations for the parameters:
The sndPlaySound function returns True (-1) if the sound is played, otherwise it returns False (0).

Code Sample

The following code example illustrates how to use the sndPlaySound API function to play a waveform (.wav) sound file. Add the code to the global module or general Declarations section of your form:

   Private  Declare Function sndPlaySound Lib "WINMM.DLL" Alias _
      "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As _
      Long) As Long
   Const SND_SYNC = &H0
   Const SND_ASYNC = &H1
   Const SND_NODEFAULT = &H2
   Const SND_LOOP = &H8
   Const SND_NOSTOP = &H10 

Add the following code to the appropriate Function or Sub procedure in your application:

   SoundName$ = "c:\windows\tada.wav"
   wFlags% = SND_ASYNC Or SND_NODEFAULT
   x% = sndPlaySound(SoundName$,wFlags%) 

NOTE: If a large waveform (.wav) sound file is specified and this call fails to play the file in its entirety, you need to adjust the settings on the appropriate sound driver.

THE FOLLOWING SECTION APPLIES TO VISUAL BASIC 3.0 ONLY

To use the sndPlaySound API from within a Visual Basic for Windows application, you must declare the sndPlaySound function in the global module or in the Declarations section of your Code window. To declare the function, enter the following Declare statement as one, single line:

   Declare Function sndPlaySound Lib "MMSYSTEM.DLL" (ByVal lpszSoundName As
      Any, ByVal wFlags%) As Integer 

Here are explanations for the parameters:
The sndPlaySound function returns True (-1) if the sound is played, otherwise it returns False (0).

Code Sample

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:

   'VB3Line: Enter the following lines as one line
   Declare Function sndPlaySound Lib "MMSYSTEM.DLL" (ByVal lpszSoundName$,
      ByVal wFlags%) As Integer
   Global Const SND_SYNC      = &H0000
   Global Const SND_ASYNC     = &H0001
   Global Const SND_NODEFAULT = &H0002
   Global Const SND_LOOP      = &H0008
   Global Const SND_NOSTOP    = &H0010 

Add the following code to the appropriate Function or Sub procedure in your application:

   SoundName$ = "c:\windows\tada.wav"
   wFlags% = SND_ASYNC Or SND_NODEFAULT
   x% = sndPlaySound(SoundName$,wFlags%) 

NOTE: If a large waveform (.wav) sound file is specified and this call fails to play the file in its entirety, you need to adjust the settings on the appropriate sound driver.


REFERENCES

"Microsoft Multimedia Development Kit: Programmer's Reference" version 1.0. 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

Additional query words:


Keywords          : kbcode kbVBp400 kbVBp500 kbhowto VB4WIN VBKBWinAPI kb32bitOnly 
Version           : WINDOWS:2.0,3.0,4.0,5.0
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 15, 1999