[RESOLVED] Wav file from resource file problem
I have added a wav file to a resource file, its in a folder called WAVE and it has an id of 101.
When I click on the command button, it does not play the sound, instead it plays a beep.
This is what I have:
VB Code:
Option Explicit
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Public Sub ButtonClickSound(cb As CommandButton)
With cb
PlaySound LoadResData(101, "WAVE"), ByVal 0&, SND_FILENAME Or SND_ASYNC
End With
End Sub
I am then calling it in the click event of the command button like so:
VB Code:
ButtonClickSound Command1
Re: Wav file from resource file problem
I even took out the reference to the command button name and I get the same result.
Re: Wav file from resource file problem
The wave will play only after you have compiled your application and run your executable. Unfourtanetly you cannot hear the wave while designing.
Compile your application and run it and your should be able to hear the wave file.
Re: Wav file from resource file problem
Quote:
Originally Posted by rami.haddad
The wave will play only after you have compiled your application and run your executable. Unfourtanetly you cannot hear the wave while designing.
Compile your application and run it and your should be able to hear the wave file.
already tried that and hear the same sound in the IDE as well as when compiled.
Re: Wav file from resource file problem
Try calling the wave file like this (must be compiled):
VB Code:
PlaySound("101", App.hInstance, SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT Or SND_ASYNC)
The SND_RESOURCE variable tells the PlaySound function that your wave is located in the ressource file.
Re: Wav file from resource file problem
Quote:
Originally Posted by rami.haddad
Try calling the wave file like this (must be compiled):
VB Code:
PlaySound("101", App.hInstance, SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT Or SND_ASYNC)
The SND_RESOURCE variable tells the PlaySound function that your wave is located in the ressource file.
before i can use that, i am going to need the constants for SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT
Private Const SND_RESOURCE = &H40004
Private Const SND_NOWAIT = &H2000
Private Const SND_NODEFAULT = &H2
Private Const SND_ASYNC = &H1
VB Code:
PlaySound("101", App.hInstance, SND_RESOURCE Or SND_NOWAIT Or SND_NODEFAULT Or SND_ASYNC)
Re: Wav file from resource file problem
Give this a try, it works for me in the IDE and as a compiled exe...
VB Code:
Option Explicit
Private Const SND_ASYNC = &H1
Private Const SND_MEMORY = &H4
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Sub Command1_Click()
PlaySound StrConv(LoadResData(101, "CUSTOM"), vbUnicode), ByVal 0&, SND_MEMORY Or SND_ASYNC
End Sub
Re: Wav file from resource file problem
I needed to do the same thing only a few weeks back the way I found is as someone already said above that it only works when compiled. but You can also play the data with Waveout API calls, but you will need to extract the wave data from the resource and add the wave header info, then you can then use the Waveout api calls, for more info on the wav file the link below it was written for C++ but is easy to understand and convert to VB
http://www.borg.com/~jglatt/tech/wave.htm
hope it may help.
Re: Wav file from resource file problem
The code I posted above (#7) works just fine in the IDE. ;)
Re: Wav file from resource file problem
yep, works perfectly. thanks pnish!!
Quote:
Originally Posted by pnish
Give this a try, it works for me in the IDE and as a compiled exe...
VB Code:
Option Explicit
Private Const SND_ASYNC = &H1
Private Const SND_MEMORY = &H4
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Private Sub Command1_Click()
PlaySound StrConv(LoadResData(101, "CUSTOM"), vbUnicode), ByVal 0&, SND_MEMORY Or SND_ASYNC
End Sub