home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "basWave"
- '*********************************************************************
- ' BASWAVE.BAS - Plays a wave file from a resource.
- '*********************************************************************
- Option Explicit
- #If Win32 Then
- Private Declare Function PlaySound Lib "winmm.dll" Alias _
- "PlaySoundA" (lpszName As Any, ByVal hModule&, _
- ByVal dwFlags As Long) As Long
- Private Declare Function sndPlaySound& Lib "winmm" Alias _
- "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long)
- '*********************************************************************
- ' Flag values for uFlags parameter.
- '*********************************************************************
- Public Const SND_SYNC = &H0 ' Play synchronously (default)
- Public Const SND_ASYNC = &H1 ' Play asynchronously
- ' **SEE NOTE IN PlayWavRes!!!!
- Public Const SND_NODEFAULT = &H2 ' No default sound event is used
- Public Const SND_MEMORY = &H4 ' lpszSoundName points to a
- ' memory file.
- Public Const SND_ALIAS = &H10000 ' Name is a WIN.INI [sounds] entry
- Public Const SND_FILENAME = &H20000 ' Name is a file name
- Public Const SND_RESOURCE = &H40004 ' Name is a resource name or atom
- Public Const SND_ALIAS_ID = &H110000 ' Name is a WIN.INI [sounds]
- ' entry identifier.
- Public Const SND_ALIAS_START = 0 ' Must be > 4096 to keep strings
- ' in same section of resource file.
- Public Const SND_LOOP = &H8 ' Loop the sound until next sndPlaySound
- Public Const SND_NOSTOP = &H10 ' Don't stop any currently playing sound
- Public Const SND_VALID = &H1F ' Valid flags
- Public Const SND_NOWAIT = &H2000 ' Don't wait if the driver is busy
- Public Const SND_VALIDFLAGS = &H17201F ' Set of valid flag bits. Anything outside
- ' this range will raise an error.
- Public Const SND_RESERVED = &HFF000000 ' In particular these flags are reserved
- Public Const SND_TYPE_MASK = &H170007
- Public Const SND_PURGE = &H40 ' Purge non-static events for task
- Public Const SND_APPLICATION = &H80 ' Look for application specific association
- #Else
- Private Declare Function sndPlaySound Lib "MMSYSTEM" ( _
- lpszSoundName As Any, ByVal wFlags%) As Integer
- '*********************************************************************
- ' Flag values for wFlags parameter.
- '*********************************************************************
- Public Const SND_SYNC = &H0 ' Play synchronously (default)
- Public Const SND_ASYNC = &H1 ' Play asynchronously
- ' **SEE NOTE IN PlayWavRes!!!!
- Public Const SND_NODEFAULT = &H2 ' Don't use default sound
- Public Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file
- Public Const SND_LOOP = &H8 ' Loop the sound until next sndPlaySound
- Public Const SND_NOSTOP = &H10 ' Don't stop any currently playing sound
- #End If
- '*********************************************************************
- ' Plays a wave file from a resource.
- '*********************************************************************
- Public Sub PlayWaveRes(vntResourceID As Variant, Optional vntFlags)
- Dim bytSound() As Byte ' Always store binary data in byte arrays!
- bytSound = LoadResData(vntResourceID, "WAVE")
- '*****************************************************************
- ' If no flags were provided, then set the defaults.
- '*****************************************************************
- If IsMissing(vntFlags) Then
- vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_MEMORY
- End If
- '*****************************************************************
- ' Make sure the SND_MEMORY bit is set.
- '*****************************************************************
- If (vntFlags And SND_MEMORY) = 0 Then
- vntFlags = vntFlags Or SND_MEMORY
- End If
- '*****************************************************************
- ' WARNING: If you want to play sound files asynchronously in
- ' Win32, then you MUST change bytSound() from a local
- ' variable to a module-level or static variable. Doing
- ' this prevents your array from being destroyed before
- ' sndPlaySound is complete. If you fail to do this, you
- ' will pass an invalid memory pointer, which will cause
- ' a GPF in MCI.
- '*****************************************************************
- If (vntFlags And SND_ASYNC) Then ' Turn off SND_ASYNC if present
- vntFlags = vntFlags Xor SND_ASYNC
- End If
- '*****************************************************************
- ' Pass the address of the first element in the byte array to play
- ' the wave file.
- '*****************************************************************
- If sndPlaySound(bytSound(0), vntFlags) = False Then
- MsgBox "PlayWaveRes failed!", vbCritical
- End If
- End Sub
-
- Public Sub PlayWaveFile(strFileName As String, Optional vntFlags)
- '*****************************************************************
- ' If no flags were provided, then set the defaults.
- '*****************************************************************
- If IsMissing(vntFlags) Then
- vntFlags = SND_NODEFAULT Or SND_SYNC
- End If
- '*****************************************************************
- ' Turn off SND_MEMORY if present.
- '*****************************************************************
- If (vntFlags And SND_MEMORY) Then
- vntFlags = vntFlags Xor SND_MEMORY
- End If
- '*****************************************************************
- ' Play the wave (BE SURE TO USE ByVal!!!!).
- '*****************************************************************
- If sndPlaySound(ByVal strFileName, vntFlags) = False Then
- MsgBox "PlayWaveFile failed!", vbCritical
- End If
- End Sub
- #If Win32 Then
- '*********************************************************************
- ' The following procedures were provided for educational purposes only.
- ' All Win32 apps should start using PlaySound because sndPlaySound
- ' is now obsolete. I chose to use sndPlaySound in this module
- ' because PlaySound is not available on Win16.
- '*********************************************************************
- Public Sub PlayWaveRes32(vntResourceID As Variant, Optional vntFlags)
- Dim bytSound() As Byte ' Always store binary data in byte arrays!
- bytSound = LoadResData(vntResourceID, "WAVE")
- '*****************************************************************
- ' If no flags were provided, then set the defaults.
- '*****************************************************************
- If IsMissing(vntFlags) Then
- vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_MEMORY
- End If
- '*****************************************************************
- ' WARNING: If you want to play sound files asynchronously in
- ' Win32, then you MUST change bytSound() from a local
- ' variable to a module-level or static variable. Doing
- ' this prevents your array from being destroyed before
- ' sndPlaySound is complete. If you fail to do this, you
- ' will pass an invalid memory pointer, which will cause
- ' a GPF in MCI.
- '*****************************************************************
- If (vntFlags And SND_ASYNC) Then ' Turn off SND_ASYNC if present
- vntFlags = vntFlags Xor SND_ASYNC
- End If
- '*****************************************************************
- ' Make sure these bits are set properly.
- '*****************************************************************
- vntFlags = IIf((vntFlags And SND_ALIAS) = 0, vntFlags, _
- vntFlags Xor SND_ALIAS)
- vntFlags = IIf((vntFlags And SND_MEMORY) = 0, vntFlags _
- Or SND_MEMORY, vntFlags)
- vntFlags = IIf((vntFlags And SND_FILENAME) = 0, vntFlags, _
- vntFlags Xor SND_FILENAME)
- '*****************************************************************
- ' Call PlaySound to play the wave file.
- '*****************************************************************
- If PlaySound(bytSound(0), 0&, vntFlags) = False Then
- MsgBox "PlayWaveFile32 failed!", vbCritical
- End If
- End Sub
-
- Public Sub PlayWaveFile32(strFileName As String, Optional vntFlags)
- '*****************************************************************
- ' If no flags were provided, then set the defaults.
- '*****************************************************************
- If IsMissing(vntFlags) Then
- vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_FILENAME
- End If
- '*****************************************************************
- ' Make sure these bits are set properly.
- '*****************************************************************
- vntFlags = IIf((vntFlags And SND_ALIAS) = 0, vntFlags, _
- vntFlags Xor SND_ALIAS)
- vntFlags = IIf((vntFlags And SND_MEMORY) = 0, vntFlags, _
- vntFlags Xor SND_MEMORY)
- vntFlags = IIf((vntFlags And SND_FILENAME) = 0, vntFlags _
- Or SND_FILENAME, vntFlags)
- '*****************************************************************
- ' Call PlaySound to play the wave file.
- '*****************************************************************
- If PlaySound(ByVal strFileName, 0&, vntFlags) = False Then
- MsgBox "PlayWaveFile32 failed!", vbCritical
- End If
- End Sub
- #End If
-
-