Help and Support

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

Article ID:86281
Last Review:July 1, 2004
Revision:3.1
This article was previously published under Q86281

THE FOLLOWING SECTION APPLIES TO VISUAL BASIC VERSIONS 4.0 AND 5.0

On This Page

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.

Back to the top

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).

Back to the top

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.

Back to the top

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).

Back to the top

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.

Back to the top

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

Back to the top


APPLIES TO
Microsoft Visual Basic 5.0 Learning Edition
Microsoft Visual Basic 5.0 Professional Edition
Microsoft Visual Basic 2.0 Professional Edition
Microsoft Visual Basic 3.0 Professional Edition
Microsoft Visual Basic 5.0 Enterprise Edition
Microsoft Visual Basic 4.0 Standard Edition
Microsoft Visual Basic 4.0 Professional Edition
Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Microsoft Visual Basic 2.0 Standard Edition
Microsoft Visual Basic 6.0 Enterprise Edition
Microsoft Visual Basic 6.0 Professional Edition

Back to the top

Keywords: 
kbhowto kb32bitonly kbcode KB86281

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.