Attribute VB_Name = "modMemoryWAV" Option Explicit '============================================================================================================= ' ' modMemoryWAV Module ' ------------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : January 4, 2002 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : Sound Card ' (This module was designed to be used with the "cResource.cls" class module, but does not require it) ' ' Description : This module allows you to play and stop any WAVE file from memory that you have previously put ' into a compiled resource DLL (ActiveX DLL or standard C DLL). This saves you the hastle and ' delay of having to load the file from a resource, save it to disk, then load it from disk and ' play it. This module uses existing drivers to play the WAVE files, so there's no need for ' dependencies, etc. ' ' Example Use: ' ' Option Explicit ' Private RES As cResource ' Private lngWave1 As Long ' Private lngWave2 As Long ' Private Sub cmdPlay1_Click() ' ResWAV_Play lngWave1, , False, False ' End Sub ' Private Sub cmdPlay2_Click() ' ResWAV_Play lngWave2, , False, True ' End Sub ' Private Sub cmdStop_Click() ' ResWAV_Stop ' End Sub ' Private Sub cmdStopAll_Click() ' ResWAV_StopAll ' End Sub ' Private Sub Form_Load() ' Dim strPath As String ' ' Get the resource DLL's file path ' strPath = App.Path ' If Right(strPath, 1) <> "\" Then strPath = strPath & "\" ' strPath = strPath & "Resource.dll" ' ' Initialize the cResource class module ' Set RES = New cResource ' RES.LibraryFilePath = strPath ' ' Get the WAVE file from the resource and play it ' RES.DLL_LoadData 101, lngWave1, , "WAVE" ' RES.DLL_LoadData 102, lngWave2, , "WAVE" ' End Sub ' Private Sub Form_Unload(Cancel As Integer) ' Set RES = Nothing ' End Sub ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Private Const SND_APPLICATION = &H80 ' The sound is played using an application-specific association. Private Const SND_ALIAS = &H10000 ' The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE. Private Const SND_ALIAS_ID = &H110000 ' The pszSound parameter is a predefined sound identifier. Private Const SND_ASYNC = &H1 ' The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL. Private Const SND_FILENAME = &H20000 ' The pszSound parameter is a filename. Private Const SND_LOOP = &H8 ' The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event. Private Const SND_MEMORY = &H4 ' A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory. Private Const SND_NODEFAULT = &H2 ' No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound. Private Const SND_NOSTOP = &H10 ' The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound. If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound. Private Const SND_NOWAIT = &H2000 ' If the driver is busy, return immediately without playing the sound. Private Const SND_PURGE = &H40 ' Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped. You must also specify the instance handle to stop SND_RESOURCE events. Private Const SND_RESOURCE = &H40004 ' The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource. Private Const SND_SYNC = &H0 ' Synchronous playback of a sound event. PlaySound returns after the sound event completes. Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal Sound As Long, ByVal hLib As Long, ByVal lngFlag As Long) As Long 'BOOL '============================================================================================================= ' ResWAV_Play ' ' Purpose : ' Starts the playback of the WAVE resource. ' ' Param Use ' ------------------------------------ ' lngPointerToWave Specifies the pointer to a spot in memory that contains the WAVE file to play. ' blnLoop Optional. If set to TRUE, the WAVE will continue to play repeatedly until the ' "ResWAV_Stop" or "ResWAV_StopAll" function is called ' blnWaitForWaveToFinish Optional. If set to true, this function will not return until the WAVE has played ' completely ' blnDontPlayIfDeviceBusy Optional. If set to true, this function will not play the specified WAVE if another ' WAVE is already playing ' ' Return ' ------ ' FALSE if error occurs ' TRUE if succeeds ' '============================================================================================================= Public Function ResWAV_Play(ByVal lngPointerToWave As Long, Optional ByVal blnLoop As Boolean = False, Optional ByVal blnWaitForWaveToFinish As Boolean = True, Optional ByVal blnDontPlayIfDeviceBusy As Boolean = False) As Boolean Dim lngFlags As Long ' Validate parameters If lngPointerToWave = 0 Then Exit Function ' Setup base flag(s) lngFlags = SND_MEMORY Or SND_NODEFAULT ' Setup custom flags If blnLoop = True Then lngFlags = lngFlags Or SND_LOOP Or SND_ASYNC If blnDontPlayIfDeviceBusy = True Then lngFlags = lngFlags Or SND_NOSTOP Or SND_NOWAIT If blnWaitForWaveToFinish = True And blnLoop = False Then lngFlags = lngFlags Or SND_SYNC ElseIf (lngFlags And SND_ASYNC) <> SND_ASYNC Then lngFlags = lngFlags Or SND_ASYNC End If ' Play the sound If PlaySound(lngPointerToWave, 0, lngFlags) = 1 Then ResWAV_Play = True End Function '============================================================================================================= ' ResWAV_Stop ' ' Purpose : ' Stops the playback of the current WAVE resource. ' ' Param Use ' ------------------------------------ ' None ' ' Return ' ------ ' FALSE if error occurs ' TRUE if succeeds ' '============================================================================================================= Public Function ResWAV_Stop() As Boolean ' Stop the sound If PlaySound(0, 0, SND_MEMORY Or SND_NODEFAULT) = 1 Then ResWAV_Stop = True End Function '============================================================================================================= ' ResWAV_StopAll ' ' Purpose : ' Stops the playback of all sounds that are playing on behalf of the calling task. ' ' Param Use ' ------------------------------------ ' None ' ' Return ' ------ ' FALSE if error occurs ' TRUE if succeeds ' '============================================================================================================= Public Function ResWAV_StopAll() As Boolean ' Stop all sounds If PlaySound(0, 0, SND_NODEFAULT Or SND_PURGE) = 1 Then ResWAV_StopAll = True End Function