How to store/retrieve .wav files in .res files
Last updated: December 09, 1997
Article ID: RS21980
The information in this article applies to:
SUMMARY
This article explains how to store .wav files in a .res (resource) file and how to load them from the file to be played at run-time.
MORE INFORMATION
Games and multimedia titles which use sound files must either distribute the extra multimedia files along with the application or embed the files within the executable. The later technique is generally more desirable because it can keep others from manipulating these files.
Visual Basic provides the capability of storing files in the application by using a resource (.res) file. Although Visual Basic provides a means for storing binary data in these files (such as .wav files), there's no clear cut way of extracting and using the files with standard VB methods.
There is however at least one method of extracting the binary .wav data from the .res file and playing it using the Windows API. The following example shows you how to create the .res file, include it in the program and extract it to play sounds.
Step-by-Step Example
Option Explicit ' multimedia functions Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long) As Long Public Const SND_ASYNC = &H1 Public Const SND_LOOP = &H8 Public Const SND_NODEFAULT = &H2 Public Const SND_SYNC = &H0 Public Const SND_NOSTOP = &H10 Public Const SND_MEMORY = &H4 Public RetValue As Long
Option Explicit
' .wav array holders
Private SoundArray() As Byte
Private Sub Form_Click()
RetValue = sndPlaySound(SoundArray(0), SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY)
End Sub
Private Sub Form_Load()
SoundArray = LoadResData("RESOURSE_ID_STRING", "WAVE")
End Sub
RESOURSE_ID_STRING WAVE DISCARDABLE "myfile.wav"
rc -r project1
Notes
The declaration of the SndPlaySound is slightly different in the case. The first parameter is changed to pass a reference value to any "any" data type. This is so that the binary array can be passed. Also the SND_MEMORY flag is added to the second parameter to tell the function that the data is in memory.
THE INFORMATION PROVIDED IN THE RABIT SOFTWARE KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. RABIT SOFTWARE DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL RABIT SOFTWARE OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF RABIT SOFTWARE 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.