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

Last reviewed: July 3, 1997
Article ID: Q86281
The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic for Windows, version 1.0

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:
  • 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 then plays the associated waveform sound file. If no entry by this name exists, it is assumed the specified name is the name of a waveform sound file. If this parameter is null, any currently playing sound is stopped. Use 0& to provide a null value.
  • uFlags specifies options for playing the sound using one or more of the following flags:

        - SND_SYNC specifies that the sound is played synchronously and the
          function does not return until the sound ends.
    

        - SND_ASYNC specifies that the sound is played asynchronously and the
          function returns immediately after beginning the sound.
    

        - SND_NODEFAULT specifies that if the sound cannot be found, the
          function returns silently without playing the default sound.
    

        - SND_LOOP specifies that the sound will continue to play continuously
          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 specifies that 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).

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:
  • 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 then plays the associated waveform sound file. If no entry by this name exists, it is assumed the specified name is the name of a waveform sound file. If this parameter is null, any currently playing sound is stopped. Use 0& to provide a null value.
  • uFlags specifies options for playing the sound using one or more of the following flags:

        - SND_SYNC specifies that the sound is played synchronously and the
          function does not return until the sound ends.
    

        - SND_ASYNC specifies that the sound is played asynchronously and the
          function returns immediately after beginning the sound.
    

        - SND_NODEFAULT specifies that if the sound cannot be found, the
          function returns silently without playing the default sound.
    

        - SND_LOOP specifies that the sound will continue to play continuously
          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 specifies that 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).

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
 

	
	


Keywords : APrgOther kbcode vb432 vb4win vb5all vb5howto VBKBWinAPI kbfasttip kbhowto
Version : 1.0 2.0 3.0 4.0 5.0
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 3, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.