|
-
Sep 21st, 2006, 07:05 AM
#1
Thread Starter
Addicted Member
[RESOLVED] VB6 Embed audio
Hi all,
I've got a program that needs a sound file to play when a button is clicked. However, I want to embed it (the music file) into the program so that:
A) I can distribute the program to other people and so that they don't need the music file to go alongside it, and
B) So there isn't a delay when the program tries to load the sound file locally or from across the internet.
I've tried using the Media Player control, and although I can get it to work, I can only seem to link to a sound file using the "url" command; If I remove or rename the sound file, it stops working. I did a search of VBforums and founf the Microsoft media controls, but I can't get it to play the song at all: I tried defining the audio file both in the MMC's properties (FileName), defining FileName in the program (in Form_load), and also using the
Code:
MMC1.Command = "C:\Filename.wav"
format. I have also tried enabling the splay, and other buttons, and they appear enabled in the regular view of VB6, but when I click run to test the program, the play button is greyed out.
Any ideas / syntax advice?
Thanks!
-
Sep 21st, 2006, 07:14 AM
#2
Frenzied Member
Re: VB6 Embed audio
Two issues here, First to add a file in the program add it as a resource.
And then you play it as you would any sound file.
-
Sep 21st, 2006, 08:06 AM
#3
Thread Starter
Addicted Member
Re: VB6 Embed audio
How do you add it as a resource?
-
Sep 21st, 2006, 08:08 AM
#4
Frenzied Member
Re: VB6 Embed audio
With the Add-in resource manager.
-
Sep 21st, 2006, 08:57 AM
#5
Thread Starter
Addicted Member
Re: VB6 Embed audio
And where might I find that? Are you referring to "References" or "Components"? Sorry, but I'm pretty new to VB.
-
Sep 21st, 2006, 09:30 AM
#6
Banned
Re: VB6 Embed audio
If sound file is a wave file then here is a good method and you won't even have to ship the control.
1) Embed your wave file into your ressource file.
Call this code:
Form Code:
VB Code:
Call PlaySound("NAME OF YOUR WAVE FILE", App.hInstance, SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT Or SND_ASYNC)
Declarations:
VB Code:
Public Const SND_ASYNC As Long = &H1 ' play asynchronously
Public Const SND_FILENAME As Long = &H20000 ' name is a file name
Public Const SND_NODEFAULT As Long = &H2 ' silence not default, if sound not found
Public Const SND_NOWAIT As Long = &H2000 ' don't wait if the driver is busy
Public Const SND_RESOURCE As Long = &H40004 ' name is a resource name or atom
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
-
Sep 21st, 2006, 10:03 AM
#7
Frenzied Member
Re: VB6 Embed audio
 Originally Posted by Arby
And where might I find that? Are you referring to "References" or "Components"? Sorry, but I'm pretty new to VB.
Go to Add-Ins/Add-In Manager, and look for VB 6 Resource Editor.
Click it and then click the Loaded/Unloaded box to load it.
Once thats donw you'll have a little green cube icon with a hand on it in your toolbar.
Click that and you'll get a small window.
Click the word "CUSTOM", then click the icon 2nd in from the right (next to the help icon) on the windows toolbar.
Navigate to your .WAV file and add it in.
Remember to click the resource editors save icon before closing it.
Likewise ... if you make any changes to your WAV file for any reason then you need to go back in to the resource editor, delete the existing entry, then re-add it, as it doesn't automatically refresh.
NOW you can reference it in your program. If you need help to do that then just shout
-
Sep 21st, 2006, 03:10 PM
#8
Thread Starter
Addicted Member
Re: VB6 Embed audio
Thanks TBO - I got the file embedded. I notice that it had to create a .RES file though, will I have to include that with the exe when it's compiled?
scirocco - Where do I declare those declarations? I've tried as global declarations, in the public function, and as a seperate private function, and they all give the error:
"Compile error: Constants, Fixed-length strings, arrays, and user-defined types are not allowed as public members of object modules"
How can I call this file to play?
-
Sep 21st, 2006, 03:11 PM
#9
Frenzied Member
Re: VB6 Embed audio
No the .res filed is compiled in to the EXE
But remember that your exe gets bigger because the whole file is included.
-
Sep 21st, 2006, 04:51 PM
#10
Thread Starter
Addicted Member
Re: VB6 Embed audio
Ahh, I see, thanks 
So now, Just wondering how to call the file, and where to put those declarations? (See last post)
-
Sep 21st, 2006, 06:58 PM
#11
Banned
Re: VB6 Embed audio
Hello Arby,
Either declare them private in your form, or declare them public in a module. Then it should work. However you can't declare them public in a form.
Keep in mind you won't here the sounds until you compile the program and run the exe using the PlaySound function.
-
Sep 22nd, 2006, 03:39 AM
#12
Frenzied Member
Re: VB6 Embed audio
To "get at" your file you need to do something like this :
VB Code:
Private Sub Form_Load()
Dim sbuffer As String, iFile As Byte
sbuffer = StrConv(LoadResData("GREEN", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\green.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
sbuffer = StrConv(LoadResData("AMBER", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\amber.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
sbuffer = StrConv(LoadResData("RED", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\red.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
imgTL(0).Picture = LoadPicture("c:\green.gif")
imgTL(1).Picture = LoadPicture("c:\amber.gif")
imgTL(2).Picture = LoadPicture("c:\red.gif")
End Sub
This doesn't use audio files but graphics files. As you can see I extract them from the resource file and then I can use them as I would if they were sat on my local drive.
You would do a similar thing with your audio file (but obviously not use the LoadPicture function !).
-
Sep 22nd, 2006, 03:41 AM
#13
Frenzied Member
Re: VB6 Embed audio
I forgot to mention ... "GREEN", "AMBER" and "RED" are what I call my files in the Resource file. By default they are given numbers strting at 101, but by right clicking and going to Properties you can assign your own more meaningful names.
-
Sep 22nd, 2006, 04:04 AM
#14
Re: VB6 Embed audio
This may be a bit excessive for what you want, but you can also use the APIs mciSendString or mciExecute to play audio/video files. If you're playing simple WAV files then PlaySound or sndPlaySound will play them directly from a resource file in memory. If you're after playing mp3, midi files etc, you can store them in a resource file and create temporary files for playback. An example is in the codebank:- VB - mciSendString - audio/video playback / video splashscreen etc from resource file
-
Sep 23rd, 2006, 05:53 PM
#15
Thread Starter
Addicted Member
Re: VB6 Embed audio
I have declared those declarations in a module, but when it comes to the line:
Code:
Call PlaySound("101", App.hInstance, SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT Or SND_ASYNC)
, It doesn't work. I have "101" there because that is it's ID in the resource editor. I've also tried using the filename ending in .wav. What is the name of the file? I have compiled the exe but it is still not playing the sound.
-
Sep 24th, 2006, 11:44 AM
#16
Thread Starter
Addicted Member
Re: VB6 Embed audio
DOes it need to reference the module in which the declarations are stored somehow?
-
Sep 24th, 2006, 01:36 PM
#17
Re: VB6 Embed audio
The "101" you have in the call should not be in quotation marks (ByVal lpszName As Long). There are also a couple of issues with PlaySound, one of which is that your app will have to be compiled before it will play sounds from a resource file. The following works without issues AFTER compilation:
VB Code:
Option Explicit
'Form level code.
'YOU MUST COMPILE TO AN EXECUTABLE TO MAKE IT WORK !!!
'-----------------------------------------------------
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As Long, ByVal hModule As Long, ByVal dwFlags As Long) As Long
'Required constants.
Const SND_ASYNC = &H1 'Play asynchronously.
Const SND_RESOURCE = &H40004 'Name is a resource name or atom.
Const SND_PURGE = &H40 'Release memory.
'Toggle playing by clicking on the form.
Dim OnOff As Boolean
Private Sub Form_Click()
'Alternate clicks on the form will start/stop playback.
Dim RetVal As Long
If OnOff = True Then
OnOff = False
'Reference nothing and purge memory.
RetVal = PlaySound(0, App.hInstance, SND_PURGE)
ElseIf OnOff = False Then
OnOff = True
'Reference the res file (101)
RetVal = PlaySound(101, App.hInstance, SND_RESOURCE Or SND_ASYNC)
End If
Caption = OnOff
End Sub
Last edited by schoolbusdriver; Sep 24th, 2006 at 01:51 PM.
Reason: Grammar !
-
Sep 24th, 2006, 04:42 PM
#18
Thread Starter
Addicted Member
Re: VB6 Embed audio
It still won't work >_<, I can't figure out what I'm doing wrong...
http://www.arbital24.com/TheProgram/
That's a link to a folder with my program forms, project file, compiled exe, resource file and sound file. Do you think you could possibly have a look and recommend what to put where, or edit it for me so that it works? (I won't actually have the sound execute on form_load, it will be when the button is clicked, but I just put it there to test it).
Thanks!
-
Sep 25th, 2006, 03:21 AM
#19
Re: VB6 Embed audio
The wav file has to be identified as such in the resource file. Double-click on the 101 in the resource editor window and replace the Type "CUSTOM" with "SOUND". You should see something like this when you've done.
Code:
[-] C:\Temp\Project1.RES
[-] "SOUND"
[+] 101
Compile and run the exe. There's a link to a very good tutorial on resource files in my signature.
-
Sep 25th, 2006, 05:55 AM
#20
Thread Starter
Addicted Member
Re: VB6 Embed audio
It works perfectly, Thank you very much! ^__^
-
Oct 5th, 2006, 12:10 AM
#21
New Member
Re: VB6 Embed audio
 Originally Posted by TheBionicOrange
To "get at" your file you need to do something like this :
VB Code:
Private Sub Form_Load()
Dim sbuffer As String, iFile As Byte
sbuffer = StrConv(LoadResData("GREEN", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\green.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
sbuffer = StrConv(LoadResData("AMBER", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\amber.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
sbuffer = StrConv(LoadResData("RED", "CUSTOM"), vbUnicode)
iFile = FreeFile
Open "C:\red.gif" For Output As iFile
Print #iFile, sbuffer
Close #iFile
imgTL(0).Picture = LoadPicture("c:\green.gif")
imgTL(1).Picture = LoadPicture("c:\amber.gif")
imgTL(2).Picture = LoadPicture("c:\red.gif")
End Sub
This doesn't use audio files but graphics files. As you can see I extract them from the resource file and then I can use them as I would if they were sat on my local drive.
You would do a similar thing with your audio file (but obviously not use the LoadPicture function !).
I am new here and also new to VB.
I have the same probelm embeding txt and mdb files. Does this method apply?
Thanks.
William
-
Oct 5th, 2006, 06:27 AM
#22
Re: VB6 Embed audio
 Originally Posted by williamTAN
I am new here...
Welcome to the forums
Now that the pleasantries are over... - you should really start a new thread as this one has already been marked as resolved.
Resource files have a limit of 64kb on individually stored files, so if your db is bigger than this you'd be better off using other methods. Text can easily be stored in the res file. If you need a tutorial, look at the link in my signature.
The following may help...
VB Code:
Option Explicit
'Add a CUSTOM resource, such as notepad.exe to a res file
'and recreate it at run-time.
Private Sub Form_Load()
MakeFile "101"
End Sub
Private Sub MakeFile(intResourceID As Integer)
'Create a file from the data in the .res file.
Dim byteBinFile() As Byte 'For assembling a binary file from the resource file.
Dim intFileNum As Integer 'File number.
On Error GoTo BadResError
'Get a file handle.
intFileNum = FreeFile
'Get the data from the resource file.
byteBinFile = LoadResData(intResourceID, "CUSTOM") 'BadResError - file too big - out of memory.
'Write the binary data to it.
Open "C:\Temp\MyNotepadTest.exe" For Binary As intFileNum
Put intFileNum, , byteBinFile
Close intFileNum
Exit Sub
BadResError:
If Err.Number = 7 Then
MsgBox "Insufficient memory. File too large."
Else
MsgBox "Unexpected error:- " & Err.Number
End If
End Sub
-
Oct 5th, 2006, 06:53 AM
#23
Re: [RESOLVED] VB6 Embed audio
As you load all your wave files into the resource file as a Custom item, its a good idea to change the ID number(101) to the wave file name e.g. explode.wav to EXPLODE and change the Type to WAVE or SOUND. Now you don't need to extract the wave file to the HD to play it.
VB Code:
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_MEMORY = &H4
Private Declare Function sndPlaySound Lib "winmm.dll" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Sub PlaySound(Soundname As String) 'ResData
Dim SoundBuffer As String
'Soundname is the name of your wave file in the resource file
'"WAVE" is the Type
SoundBuffer = StrConv(LoadResData(Soundname, "WAVE"), vbUnicode)
Call sndPlaySound(SoundBuffer, SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT)
End Sub
Private Sub Command1_Click()
Call PlaySound("EXPLODE")
End Sub
Last edited by Keithuk; Oct 26th, 2006 at 06:58 AM.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Oct 26th, 2006, 06:52 AM
#24
Re: VB6 Embed audio
 Originally Posted by schoolbusdriver
The following works without issues AFTER compilation:
'YOU MUST COMPILE TO AN EXECUTABLE TO MAKE IT WORK !!!
Files you put in a resource file will be used in IDE you don't have to compile first.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Oct 26th, 2006, 07:07 AM
#25
Addicted Member
Re: [RESOLVED] VB6 Embed audio
Why didt you just do this, put the music file in an OLE and do this code
VB Code:
Private Sub Command1_Click()
OLE1.DoVerb 0
End Sub
Thats very simple.
-
Oct 26th, 2006, 02:27 PM
#26
Re: VB6 Embed audio
 Originally Posted by Keithuk
Files you put in a resource file will be used in IDE you don't have to compile first.
That's what I thought once, too. Try the following code. In the IDE, attempting to play the sounds stored in the res file fails, while the files on the HDD play OK. Different res/HDD file combinations give the same results.
When compiled to an exe, they all play.
The oddity here is "102-nudge.wav", which only plays ONCE from the res file, whether you use PlaySound or sndPlaySound. No such problem with "101-tada.wav". Wierd.
Other files give varying results too.
VB Code:
Option Explicit
'Put 4 command buttons on a form (NOT in an array).
'Sound files used: Put in the project's folder, AND the resource file.
'"101-tada.wav" Res ID 101 - formerly "C:\Windows\Media\tada.wav"
'"102-nudge.wav" Res ID 102 - formerly "C:\Program Files\MSN Messenger\nudge.wav"
'"103-wmpaud1.wav" Res ID 103 - formerly "C:\WINDOWS\Help\Tours\WindowsMediaPlayer\Audio\Wav\wmpaud1.wav"
'"104-Mwiip.wav" Res ID 104 - external source. Find your own :-)
'Note the different ways you can call PlaySound & sndPlaySound, depending
'on whether you want to pass a number or string.
'PlaySound.
'Pass a String reference to lpszName.
Private Declare Function PlaySoundString Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
'Pass a Long Resource reference to lpszName.
Private Declare Function PlaySoundResource Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As Long, ByVal hModule As Long, ByVal dwFlags As Long) As Long
'sndPlaySound.
'Pass a String reference to lpszSoundName.
Private Declare Function sndPlaySoundString Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
'Pass a Long Resource reference to lpszSoundName.
Private Declare Function sndPlaySoundLong Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As Long, ByVal uFlags As Long) As Long
'Required constants.
Const SND_ASYNC = &H1 'Play asynchronously.
Const SND_FILENAME = &H20000 'Name is a file name.
Const SND_PURGE = &H40 'Release memory.
Const SND_RESOURCE = &H40004 'Name is a resource name or atom.
Private Sub Form_Load()
'Add proper captions to the command buttons.
Command1.Caption = "PlaySound - Resource"
Command2.Caption = "PlaySound - HDD"
Command3.Caption = "sndPlaySound - Resource"
Command4.Caption = "sndPlaySound - HDD"
End Sub
Private Sub Command1_Click()
'PlaySound. Resource file. 104-Mwiip.wav
Dim RetVal As Long
'Reference nothing and purge memory.
RetVal = PlaySoundResource(0, App.hInstance, SND_PURGE)
'Reference the res file and ... Top Of The Pops ...
RetVal = PlaySoundResource(104, App.hInstance, SND_RESOURCE Or SND_ASYNC)
End Sub
Private Sub Command2_Click()
'PlaySound. HDD file. 101-tada.wav
Dim RetVal As Long
'Reference nothing and purge memory.
RetVal = PlaySoundString("", App.hInstance, SND_PURGE)
'Reference the res file and ... Top Of The Pops ...
RetVal = PlaySoundString(App.Path & "\101-tada.wav", App.hInstance, SND_FILENAME Or SND_ASYNC)
End Sub
Private Sub Command3_Click()
'sndPlaySound. Resource file. 103-wmpaud1.wav
Dim RetVal As Long
'Reference nothing and purge memory.
RetVal = sndPlaySoundLong(0, SND_PURGE)
'Reference the res file and ... Top Of The Pops ...
RetVal = sndPlaySoundLong(103, SND_RESOURCE Or SND_ASYNC)
End Sub
Private Sub Command4_Click()
'sndPlaySound. HDD file. 102-nudge.wav
Dim RetVal As Long
'Reference nothing and purge memory.
RetVal = sndPlaySoundString("", SND_PURGE)
'Reference the HDD file.
RetVal = sndPlaySoundString(App.Path & "\102-nudge.wav", SND_FILENAME Or SND_ASYNC)
End Sub
-
Oct 26th, 2006, 03:07 PM
#27
Re: [RESOLVED] VB6 Embed audio
Ok I've pasted you code into an app and added the resources. Only Command buttons 2 and 4 play sounds. I compiled the exe and still the same only 2 and 4 play.
Like I said before your previous code doesn't play sounds either.
Last edited by Keithuk; Oct 26th, 2006 at 05:27 PM.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Oct 26th, 2006, 05:20 PM
#28
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|