In this tutorial you will learn how to play sound in your Visual Basic application with a .RES file. I have included a Resource file with 8 sounds in the source. |
| First we need to create a .res file and add our .wav files to it. |
The first thing you have to do is open your project that you want to add a resource to. Then, you have to load the "Resource Editor" Add-In. To do this, go to Visual Basic's "Add-Ins" menu and select "Add-In Manager". In Visual Basic 6, select "VB 6 Resource Editor" and check the box that says "Loaded/Unloaded"
Now that you've done that, add a resource (.RES) file to your project by right-clicking on VB's "Project Window" and selecting "Resource File" from the "Add" pop-up menu.
Once you've done this, it will ask you what .RES file to open. If you have an existing .RES file you wish to add, find it and select it. Otherwise, type in the name of the NEW resource file to create. It will ask you if you want to create the .RES file... say "YES". Now you'll notice that your newly created .RES file exists in your project window under "Related Documents". If you double click on it, it will bring up the actual editor window where you can add "String Tables", "Cursors", "Icons", "Bitmaps", or "Custom Resources" |
Click on Custom Resources and locate your sound file that you would like to add and add it to the resource file. You will see it is assigned a number. Double click on that number to change its properties. For this tutorial and inside this RES File you will see the following .res IDs. CUSTOM: 101, 103, 104, 105, 107, 108. SOUND: 1, 2
Once you are done adding your sound click save and close the resource editor. Go back to Add-in manager and uncheck Resource Editor so its not loaded anymore. |
| So far we have a Form and .RES in our project. Now we need a .bas (module) in our project. Goto the top menu and click PROJECT>ADD MODULE: NEW. Double click on your new module and we will add the following code to it to play the sounds in our .res file. |
Private Declare Function PlaySoundData Lib "winmm.dll" Alias "PlaySoundA" (lpData As Any, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_MEMORY = &H4
Private Const SND_STOP = &H4
Private m_snd() As Byte |
Public Function PlaySounds2(ByVal SoundID As Long) As Long
Const FLAGS = SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT
m_snd = LoadResData(SoundID, "CUSTOM") ' refers to the CUSTOM name in the .res file '
PlaySounds2 = PlaySoundData(m_snd(0), 0, FLAGS)
End Function |
Public Function PlaySounds(ByVal SoundID As Long) As Long
Const FLAGS = SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT
m_snd = LoadResData(SoundID, "SOUND") ' refers to the SOUND name in the .res file '
PlaySounds = PlaySoundData(m_snd(0), 0, FLAGS)
End Function |
Go back to the design area in project and we can began adding our code, it's very simple to do because we have our module.
Add 8 buttons and name them whatever you want to test the sound. Double click on a button and it brings you to the code part of our project. Simply add this code and it will execute the sound that you added. Please make sure that you notice in this tutorial, PlaySounds(CUSTOM) and PlaySounds2(SOUND) |
Private Sub Command1_Click()
PlaySounds (1) ' This is tied to PlaySounds(SOUND) '
End Sub
Private Sub Command2_Click()
PlaySounds (2) ' This is tied to PlaySounds(SOUND) '
End Sub |
Private Sub Command3_Click()
PlaySounds2 (101) ' This is tied to PlaySounds2(CUSTOM) '
End Sub
Private Sub Command4_Click()
PlaySounds2 (103) ' This is tied to PlaySounds2(CUSTOM) '
End Sub
Private Sub Command5_Click()
PlaySounds2 (104) ' This is tied to PlaySounds2(CUSTOM) '
End Sub
Private Sub Command6_Click()
PlaySounds2 (105) ' This is tied to PlaySounds2(CUSTOM) '
End Sub
Private Sub Command7_Click()
PlaySounds2 (107) ' This is tied to PlaySounds2(CUSTOM) '
End Sub
Private Sub Command8_Click()
PlaySounds2 (108) ' This is tied to PlaySounds2(CUSTOM) '
End Sub |
| Thats it. Now you can play custom sounds from a .res file in your project. |
|
|
#
1323
|
Play Sounds From .RES File :: by __Doc_ |
Source code by __Doc_, The source code can be found on the source code page. |
| partial .res creation snippets from thevbzone |
|