Source code for Issue Number 29

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module and change the name of the module to basSounds

'----------------------------------------------------------------------
'
'   Module Name:    basSounds
'   Written By:     C&D Programming Corp.
'   Create Date:    3/98
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Public Enum sndPlaySoundFlags
   sndSync = &H0        ' specifies that the sound is played
                        ' synchronously and the function does not
                        ' return until the sound ends.
   sndAsync = &H1       ' specifies that the sound is played
                        ' asynchronously and the function returns
                        ' immediately after beginning the sound.
   sndNoDefault = &H2   ' specifies that if the sound cannot be
                        ' found, the function returns silently
                        ' without playing the default sound.
   sndLoop = &H8        ' specifies that the sound will continue
                        ' to play continuously until sndPlaySound
                        ' is called again with the lpszSoundName
                        ' parameter set to vbNullString. You must
                        ' also specify the sndAsync flag to loop
                        ' sounds.
   sndNoStop = &H10     ' specifies that if a sound is currently
                        ' playing, the function will immediately
                        ' return False without playing the requested
                        ' sound.
End Enum

'
' The sndPlaySound function returns True (-1) if the sound is
' played, otherwise it returns False (0).
'
Private Declare Function sndPlaySound Lib "WINMM.DLL" Alias "sndPlaySoundA" _
            (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
'
'   Plays any wave file specified by sSoundName.  By default
'   it will play the sound synchronously.  You can override this
'   behavior by using the lFlags parameter.
'
Public Sub SoundPlay(sSoundName As String, _
                        Optional lFlags As sndPlaySoundFlags = _
                        sndSync)
    Dim lReturnValue As Long

    lReturnValue = sndPlaySound(sSoundName, lFlags)
    ' if lReturnValue = 2 then the wave file probably does not
    ' exist.
End Sub
'
'   Play a sound forever.  If you want to stop the
'   sound from playing you need to call StopSound
'
Public Sub SoundPlayForever(sSoundName As String)
    SoundPlay sSoundName, sndAsync Or sndLoop
End Sub
'
'   Stops a sound that was played asynchronously
'
Public Sub SoundStop()
    SoundPlay vbNullString, sndAsync
End Sub