PDA

Click to See Complete Forum and Search --> : Q: Sound files inside the exe


Steve Stunning
Dec 1st, 1999, 09:40 AM
How do some programs have sound files inside the exe? I just played a game that was nothing but an exe and there were no sound files.

How can I make an exe and have the sound files inside the exe instead of having to access them?

Thank for reading this and any help you can provide.

------------------
Steve Stunning

Serge
Dec 1st, 1999, 06:06 PM
You can create a resource file that will be compiled with your EXE. Resource file can hold a lot of different file formats. If you have VB6, then you have a Resource Editor.
To put the file in the resource: From the Add-Ins menu select Add-In Manager.... From the list select (put a check mark) a VB6 Resource Editor. Then, from the Project menu select Add New Resource File. A common dialog will be shown to give a name to your new resource file, do so (don't worry, this file will not be needed after you compile your program). Now, you will notice that in project explorer you would have a resource file added. Now, double click on that file. You will get the Resource Editor. From the toolbar select secon icon from the right (the icon called Add Custom Resource... Select the appropriate sounds (Wavs). Notice that editor will create an ID for each item you add. You can keep the default or change them to different ID by double click on each item. Close the Resource Editor. Let's say you keep the default IDs. Now, the programming part.

Here is the generic routine that you can use:

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const FLAGS = SND_ASYNC And SND_NODEFAULT
Public Sub PlayWavResource(pintResourceID As Integer)
Dim bytArr() As Byte
Dim intFFN As Integer
Dim strPath As String

bytArr = LoadResData(pintResourceID, "CUSTOM")
strPath = App.Path & IIf(Right(App.Path, 1) = "\", "~tempwav.wav", "\~tempwav.wav")
intFFN = FreeFile
Open strPath For Binary As intFFN
Put #intFFN, , bytArr
Close #intFFN
Call sndPlaySound(strPath, FLAGS)
Kill strPath
End Sub



Usage: PlayWavResource ResourceID
Example: Call PlayWavResource(101)


------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)



[This message has been edited by Serge (edited 12-02-1999).]

chrisjk
Dec 1st, 1999, 07:25 PM
Serge - or anyone - how could I modify the above to include an AVI in my EXE that plays using the Windows Media Player? I have the AVI as resource id 101 in the CUSTOM bit.

Thank you

Regards,

------------------
- Chris
chris.kilhams@btinternet.com
If it ain't broke - don't fix it :)

Serge
Dec 1st, 1999, 07:52 PM
Sure!


Public Sub PlayAVIResource(pintResourceID As Integer, pMediaPlayer As MediaPlayer)
Dim bytArr() As Byte
Dim intFFN As Integer
Dim strPath As String

bytArr = LoadResData(pintResourceID, "CUSTOM")
strPath = App.Path & IIf(Right(App.Path, 1) = "\", "~tempavi.avi", "\~tempavi.avi")
intFFN = FreeFile
Open strPath For Binary As intFFN
Put #intFFN, , bytArr
Close #intFFN
pMediaPlayer.FileName = strPath
pMediaPlayer.Play
End Sub


Example: Call PlayAVIResource(101, MediaPlayer1)

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

Steve Stunning
Dec 3rd, 1999, 08:20 PM
Thank you Serge... I will give it a try. :)

funkheads
Dec 4th, 1999, 03:02 PM
so is there any way to compile the EXE and write ActiveX files inside of it so that the user does not have to have them located on his/her hard drive? ...and so i don't have to package them with the software? because we all know that ActiveX references can be a ***** if the user does not have them in their Windows\System directory. ok. bye.

--michael

Steve Stunning
Dec 4th, 1999, 09:42 PM
Serge,

It worked great. Thanks for you help!!

Frans C
Dec 4th, 1999, 10:30 PM
There is no need to write the wav file to disk before playing it.


Option Explicit
Private Declare Function sndPlaySound Lib "winmm" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1 ' Play asynchronously
Private Const SND_NODEFAULT = &H2 ' Don't use default sound
Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim retVal As Long
Dim SoundBuffer As String
SoundBuffer = StrConv(LoadResData(101, "CUSTOM"), vbUnicode) 'replace 101 with your resource ID
retVal = sndPlaySound(SoundBuffer, SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY)
End Sub

Steve Stunning
Dec 5th, 1999, 08:30 AM
Frans C,

Even better.... I like not having to save it to disk prior to play!!

Many thanks to you...

VB Starter
Dec 9th, 1999, 06:09 AM
since im kinda confused by this, does anyone know how to run an exe. file from a resource file? They are default ( "Custom",101) settings.

Serge
Dec 9th, 1999, 05:53 PM
Use the method I posted earlier (which is to save the file to disk first), then use Shell statement:

Public Sub RunResourceExe(pintResourceID As Integer)
Dim bytArr() As Byte
Dim intFFN As Integer
Dim strPath As String

bytArr = LoadResData(pintResourceID, "CUSTOM")
strPath = App.Path & IIf(Right(App.Path, 1) = "\", "~tempexe.exe", "\~tempexe.exe")
intFFN = FreeFile
Open strPath For Binary As intFFN
Put #intFFN, , bytArr
Close #intFFN
Shell strPath, vbNormalFocus
End Sub


Example: RunResourceExe 101

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)



[This message has been edited by Serge (edited 12-10-1999).]

Shadowlord
Mar 7th, 2000, 12:53 AM
1. Is there any way to write into a RES file from my app? My game uses RES files for levels and graphics, and I want to make an editor for it.

2. Is there any way to (probably with the API) read from a RES file other than the one that is associated with my app? I want to have a separate RES file for each chapter of my game, so I can easily create add-ons.