Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

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

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a new module to the project.
  3. Place the following code in the new module:
    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
    
    
    
    
  4. Paste the following code into the form (Form1) code editor:
    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
    
    
    
    
  5. Start your favorite text editor (such as notepad)
  6. Enter the following text:
    
    
    RESOURSE_ID_STRING	WAVE	DISCARDABLE	"myfile.wav"
    
    
  7. Save the file to your working directory. Name it "Project1.rc"
  8. Copy the VB resource compiler files (Rc.exe and Rcdll.dll) from the VB CD to your working directory.
  9. Get to the command prompt (DOS Shell) and navigate to your working directory.
  10. At the command prompt type:
    rc -r project1
    
    
  11. Back in your VB project add the newly created "project.res" file to your project.
  12. Save the project.
  13. Run the program (or press the F5 key). Each time you click on the form the .wav file is played.

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.