Results 1 to 16 of 16

Thread: Playing Sound In VB6 Application ? ? ?

  1. #1

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    Question 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
    Last edited by yarick; Dec 7th, 2005 at 08:32 PM. Reason: forgot somethin

  2. #2
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    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

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. Option Explicit
    2.  
    3. Private Const SND_APPLICATION = &H80         ' look for application specific association
    4. Private Const SND_ALIAS = &H10000     ' name is a WIN.INI [sounds] entry
    5. Private Const SND_ALIAS_ID = &H110000    ' name is a WIN.INI [sounds] entry identifier
    6. Private Const SND_ASYNC = &H1         ' play asynchronously
    7. Private Const SND_FILENAME = &H20000     ' name is a file name
    8. [COLOR=Red]Private Const SND_LOOP = &H8         ' loop the sound until next sndPlaySound
    9. [/COLOR]Private Const SND_MEMORY = &H4         ' lpszSoundName points to a memory file
    10. Private Const SND_NODEFAULT = &H2         ' silence not default, if sound not found
    11. Private Const SND_NOSTOP = &H10        ' don't stop any currently playing sound
    12. Private Const SND_NOWAIT = &H2000      ' don't wait if the driver is busy
    13. Private Const SND_PURGE = &H40               ' purge non-static events for task
    14. Private Const SND_RESOURCE = &H40004     ' name is a resource name or atom
    15. Private Const SND_SYNC = &H0         ' play synchronously (default)
    16. Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    17. Dim flag As Boolean
    18.  
    19. Private Sub Command1_Click()
    20. If Not flag Then
    21.   Command1.Caption = "Stop"
    22. [COLOR=Red]  PlaySound "C:\WINDOWS\MEDIA\TADA.WAV", ByVal 0&, SND_ASYNC Or SND_LOOP
    23. [/COLOR]Else
    24.   Command1.Caption = "Play"
    25.   PlaySound "C:\WINDOWS\MEDIA\TADA.WAV", ByVal 0&, SND_MEMORY
    26. End If
    27. flag = Not flag
    28. End Sub
    29.  
    30. Private Sub Form_Load()
    31.     'KPD-Team 2000
    32.     'URL: [url]http://www.allapi.net/[/url]
    33.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    34.  Command1.Caption = "Play"
    35. End Sub

  4. #4

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    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

  5. #5
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    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 !
    Attached Files Attached Files

  6. #6
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Playing Sound In VB6 Application ? ? ?

    try this,

    VB Code:
    1. Option Explicit
    2. Private Const SND_ASYNC     As Long = &H1
    3. Private Const SND_MEMORY    As Long = &H4
    4. Private Const SND_NODEFAULT = &H2
    5. Private Const Flags& = SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY
    6. Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
    7.                                                                              ByVal uFlags As Long) As Long
    8. Private Sub Command1_Click()
    9.  
    10. Dim b As String
    11.     b = StrConv(LoadResData(101, "custom"), vbUnicode)
    12.     sndPlaySound b, Flags&
    13.    
    14. End Sub

  7. #7

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    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

  8. #8
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    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

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  10. #10

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    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

  11. #11

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    Exclamation 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... 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?

    //Code from Main Form, the only window:
    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Dim Wav(16 To 16) As String                           'make a string array to hold 4 Wav's
    5.  
    6.  
    7. Private Sub Form_Load()
    8.  
    9.  
    10. ' the next line plays the WAV from the RES file, not from memory
    11. Call PlayRESSound(16)
    12.                                                       'you could also say: "Call PlayRESSound(16, True)"  to force your
    13.                                                       'program to wait until the WAV is done playing
    14. Dim i%
    15. For i = 16 To 16                                      'loop through
    16. Wav(i) = StrConv(LoadResData(i, "SOUND"), vbUnicode)  'load the WAV's into a string array
    17.                      'add them to the listbox
    18. Next i
    19. End Sub
    20.  
    21. Private Sub Form_Unload(Cancel As Integer)
    22. sndPlaySound vbNullString, SND_ASYNC                   'stop playing sound (otherwise will it will be played forever)
    23. End Sub
    24.  
    25. Private Sub cmdExit_Click()
    26.  
    27. Unload Me
    28.  
    29. End
    30.  
    31. End Sub

    //AND this is code from my only Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
    4.  
    5. Public Const SND_SYNC = &H0         '  play synchronously  (default)
    6. Public Const SND_ASYNC = &H1        '  play asynchronously (the program does not stop for the WAV)
    7. Public Const SND_MEMORY = &H4       '  play from memory    (from a String)
    8. Public Const SND_LOOP = &H8         '  loop the sound until next sndPlaySound
    9. Public Const SND_NOSTOP = &H10      '  don't stop any currently playing sound
    10.  
    11.  
    12. ' I got this sub from Kalani, very nicely done!
    13. Public Sub PlayRESSound(iIndex As Integer, Optional bWait As Boolean)
    14. On Error Resume Next                '  just in case
    15. Dim lFlags&
    16. If bWait = True Then
    17.     lFlags = SND_SYNC Or SND_MEMORY Or SND_LOOP
    18.   Else
    19.     lFlags = SND_ASYNC Or SND_MEMORY Or SND_LOOP
    20. End If
    21. Dim vAddress$
    22. ' the next line does all the work.
    23. vAddress = StrConv(LoadResData(iIndex, "SOUND"), vbUnicode)
    24. sndPlaySound vAddress, lFlags
    25. End Sub
    26.  
    27.  
    28.  
    29. Sub Main()
    30.  
    31. End Sub

    Thanx for ur time guys i really appreciate it

  12. #12
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    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 !

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  14. #14
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    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
    Attached Files Attached Files

  15. #15

    Thread Starter
    New Member yarick's Avatar
    Join Date
    Dec 2005
    Location
    C:\WINDOWS\Repair\Sam
    Posts
    7

    Resolved 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


  16. #16
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Thumbs up 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width