Playing Sound In VB6 Application ? ? ?
Ok, I got my litl porgram done now, but at the end i realized that i need to play sound constantly all the time my program runs. I've found one way to do it by adding this script into main form:
Private Sub form_load()
'This script could be changed to the Command1_Click event as well or any event you want to play your sound on though i need it the whole time
Dim i As Long
Const SoundFileName$ = "C:\Documents and Settings\Owner\My Documents\My Music\binacard_1.wav"
i = waveOutGetNumDevs()
If i > 0 Then
i& = sndPlaySound(SoundFileName$, Flags&)
Else
Beep
End If
End Sub
And also i have to declare funct. in separate module:
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Declare Function waveOutGetNumDevs Lib "winmm" () As Long
Global Const SND_SYNC = &H0 'just after the sound is ended exit function
Global Const SND_ASYNC = &H1 'just after the beginning of the sound exit function
Global Const SND_NODEFAULT = &H2 'if the sound cannot be found no error message
Global Const SND_LOOP = &H8 'repeat the sound until the function is called again
Global Const SND_NOSTOP = &H10 'if currently a sound is played the function will return without playing the selected sound
Global Const Flags& = SND_ASYNC Or SND_NODEFAULT
So, as the result I get that soundfile, "binacard1.wav" playing in the background whenever my program starts (it only has one main window).
Though My Questions are:
1. How can i include that soundfile in my .exe file? Is it possible at all?
2. If not, Is there any way i can get my program to play it from internet? (file is ~300kb and i already uploaded it on http://yarick.animaze.net/binacard_1.wav)
3. I also need to LOOP playing of this sound, so that it's being played the whole time until u terminate the program. Is it Possible at all, and if yes - what do i change in my script? Do i have to use another one in order to do that?
//So the whole point is to get compact with the sound (so i only have one .exe file and nothing else and so it works the same way on any PC), and LOOP it.
THANX FOR HELP IN ADVANCE!
i'm not really experienced in VB6 so don't mind me if something i've asked is really simple
Re: Playing Sound In VB6 Application ? ? ?
hmm from the web embed a mediaplayer and in custom settings in the properties menu
you can enter your wav url and select how many times it plays
or off form
with >
WindowsMediaPlayer1.URL= "http://yarick.animaze.net/binacard_1.wav"
WindowsMediaPlayer1.Controls.play
if you find an easy example of embedding a wav into an exe please post it as the only example i have of doing it is not verry help full
Re: Playing Sound In VB6 Application ? ? ?
1) Search for Resource File to embed a file in your program.
2) See #1
3) You can get the song to loop by adding a flag. See this sample that I've modified slightly. Still included the allapi.net comments, though.
VB Code:
Option Explicit
Private Const SND_APPLICATION = &H80 ' look for application specific association
Private Const SND_ALIAS = &H10000 ' name is a WIN.INI [sounds] entry
Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI [sounds] entry identifier
Private Const SND_ASYNC = &H1 ' play asynchronously
Private Const SND_FILENAME = &H20000 ' name is a file name
[COLOR=Red]Private Const SND_LOOP = &H8 ' loop the sound until next sndPlaySound
[/COLOR]Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file
Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found
Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound
Private Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy
Private Const SND_PURGE = &H40 ' purge non-static events for task
Private Const SND_RESOURCE = &H40004 ' name is a resource name or atom
Private Const SND_SYNC = &H0 ' play synchronously (default)
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Dim flag As Boolean
Private Sub Command1_Click()
If Not flag Then
Command1.Caption = "Stop"
[COLOR=Red] PlaySound "C:\WINDOWS\MEDIA\TADA.WAV", ByVal 0&, SND_ASYNC Or SND_LOOP
[/COLOR]Else
Command1.Caption = "Play"
PlaySound "C:\WINDOWS\MEDIA\TADA.WAV", ByVal 0&, SND_MEMORY
End If
flag = Not flag
End Sub
Private Sub Form_Load()
'KPD-Team 2000
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [email]KPDTeam@Allapi.net[/email]
Command1.Caption = "Play"
End Sub
Re: Playing Sound In VB6 Application ? ? ?
Quote:
Originally Posted by Rattled_Cage
...
if you find an easy example of embedding a wav into an exe please post it as the only example i have of doing it is not verry help full
yea i sure will; remember editing one of the windows' components (i think it was that game, minesweeper) with ResourceHacker i and i saw embedded .wav FOR sure! though i dont think its easy to embed a wav thru standard vb6 interface.
Anyways, can u plz post ur example of embeddin a wav even if its not helpful at all - anything is better than nothin.
YEA, and can u give me more details how to include that part with WMP controls?
Thanx a lot
1 Attachment(s)
Re: Playing Sound In VB6 Application ? ? ?
ok uploaded it here i think ><>
http://www.vbforums.com/attachment.p...id=43361&stc=1
somewhere in there it explains to go to a dos promt and type rc /embed your own exe .. worked for me once !
Re: Playing Sound In VB6 Application ? ? ?
try this,
VB Code:
Option Explicit
Private Const SND_ASYNC As Long = &H1
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT = &H2
Private Const Flags& = SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Sub Command1_Click()
Dim b As String
b = StrConv(LoadResData(101, "custom"), vbUnicode)
sndPlaySound b, Flags&
End Sub
Re: Playing Sound In VB6 Application ? ? ?
Thank you all guys, so far every advice has been helpful and it seems to be working fine, i didnt get it to loop yet, the main task is to embed the wav though, so THANK YOU ALL! Appreciate ur help:)
Re: Playing Sound In VB6 Application ? ? ?
The sndPlaySound API unfortunately is very limited, and can only play one sound at a time. If you are making a program, such as a game, that requires sounds to play more sounds at the same time, then you can either use DirectSound, or WAVEMIX32. DirectSound gives you more control though, and can do streaming audio. Heres a nice tutorial on it:
http://directx4vb.vbgamer.com/DirectX4VB/TUT_DX7_DS.asp
http://directx4vb.vbgamer.com/DirectX4VB/TUT_DX8_DA.asp
Re: Playing Sound In VB6 Application ? ? ?
Quote:
Originally Posted by yarick
Thank you all guys, so far every advice has been helpful and it seems to be working fine, i didnt get it to loop yet, the main task is to embed the wav though, so THANK YOU ALL! Appreciate ur help:)
Did you try my sample? There is a second delay at the end of the wave, but it starts right after that, and loops until you press the button again.
Re: Playing Sound In VB6 Application ? ? ?
Jacob Roman
yea i realize that using that api is the most poor way to play - though im workin on somettin really small and i just need to play a .wav in the background on da loop, so ii guess sndPlaySound is suiteable for that. THANKS for ur help though
Re: Playing Sound In VB6 Application ? ? ?
Quote:
Originally Posted by dglienna
Did you try my sample? There is a second delay at the end of the wave, but it starts right after that, and loops until you press the button again.
ok, I did try your sample, that WAS SO WHAT I NEEDED!! Thanks man!
I got my program to play the sound, loop (0.5 sec pause is noticeable but its okay), so whenever i run it in VB6 (i choose "run with full compile") it runs PERFECT and the cound is played. But when i compile an .exe (size of ~800kb with sound embedded) it starts up fine with all the grafix and everything i need but THERE IS NO SOUND PLAYED... :confused: i have no clue what could that be, in VB6 everything works fine, and when i compile final .exe it doesn't show any errors....
ANY ideas?:):D
//Code from Main Form, the only window:
VB Code:
Option Explicit
Dim Wav(16 To 16) As String 'make a string array to hold 4 Wav's
Private Sub Form_Load()
' the next line plays the WAV from the RES file, not from memory
Call PlayRESSound(16)
'you could also say: "Call PlayRESSound(16, True)" to force your
'program to wait until the WAV is done playing
Dim i%
For i = 16 To 16 'loop through
Wav(i) = StrConv(LoadResData(i, "SOUND"), vbUnicode) 'load the WAV's into a string array
'add them to the listbox
Next i
End Sub
Private Sub Form_Unload(Cancel As Integer)
sndPlaySound vbNullString, SND_ASYNC 'stop playing sound (otherwise will it will be played forever)
End Sub
Private Sub cmdExit_Click()
Unload Me
End
End Sub
//AND this is code from my only Module:
VB Code:
Option Explicit
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_SYNC = &H0 ' play synchronously (default)
Public Const SND_ASYNC = &H1 ' play asynchronously (the program does not stop for the WAV)
Public Const SND_MEMORY = &H4 ' play from memory (from a String)
Public Const SND_LOOP = &H8 ' loop the sound until next sndPlaySound
Public Const SND_NOSTOP = &H10 ' don't stop any currently playing sound
' I got this sub from Kalani, very nicely done!
Public Sub PlayRESSound(iIndex As Integer, Optional bWait As Boolean)
On Error Resume Next ' just in case
Dim lFlags&
If bWait = True Then
lFlags = SND_SYNC Or SND_MEMORY Or SND_LOOP
Else
lFlags = SND_ASYNC Or SND_MEMORY Or SND_LOOP
End If
Dim vAddress$
' the next line does all the work.
vAddress = StrConv(LoadResData(iIndex, "SOUND"), vbUnicode)
sndPlaySound vAddress, lFlags
End Sub
Sub Main()
End Sub
Thanx for ur time guys i really appreciate it :wave:
Re: Playing Sound In VB6 Application ? ? ?
http://www.vbaccelerator.com/home/VB...XE/article.asp
works well in making the res file ! from cmd
but still a lil lost !
Re: Playing Sound In VB6 Application ? ? ?
I don't think you can play directly from the resource file, but would have to extract it to a file in order to play it. I'm not positive about it though. Maybe you could post in the Games Forum. They'd know.
1 Attachment(s)
Re: Playing Sound In VB6 Application ? ? ?
theres a read me inside to expain a lil
you can go get the RC.EXE RCDLL.DLL
your self if it insnt already hiding from you
Re: Playing Sound In VB6 Application ? ? ?
Quote:
Originally Posted by Rattled_Cage
theres a read me inside to expain a lil
you can go get the RC.EXE RCDLL.DLL
your self if it insnt already hiding from you
Yea that thing works out fine, i got it to work, and make me a resource file, its really simple to use
Re: Playing Sound In VB6 Application ? ? ?
Quote:
Originally Posted by Rattled_Cage
hmm from the web embed a mediaplayer and in custom settings in the properties menu
you can enter your wav url and select how many times it plays
or off form
with >
WindowsMediaPlayer1.URL= "http://yarick.animaze.net/binacard_1.wav"
WindowsMediaPlayer1.Controls.play
if you find an easy example of embedding a wav into an exe please post it as the only example i have of doing it is not verry help full
Rattled_Cage, I wish I would have found this earlier. This worked perfectly! It's almost like you wrote it for me.:D