Results 1 to 24 of 24

Thread: [RESOLVED] Looping a .wav File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Resolved [RESOLVED] Looping a .wav File

    I can make it loop, but how do I make it Stop.
    Here is my code, this is in a Module
    Code:
    Option Explicit
    
    Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
             (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
     
    
    
    Private Const SND_SYNC = &H0
    Private Const SND_ASYNC = &H1
    Private Const SND_NODEFAULT = &H2
    Private Const SND_LOOP = &H8
    Private Const SND_NOSTOP = &H10
    Private Const wFlags = SND_ASYNC Or SND_LOOP
    
    Dim X%
    
    Public Sub Play_LOOP(wavFile  as String)
      X = sndPlaySound (wavFile , wFlags)
    End Sub
    
    Public Sub StopPlay_LOOP(wavFile  as String)
      X = sndPlaySound( wavFile, SND_ASYNC )    'wFlags   SND_ASYNC
    End Sub
    The Form
    Code:
    Option Explicit
    
      Dim myFileName$
      
     Private Sub Form_Load()
            myFileName = App.Path & "\tmpClick.wav"
            Play_LOOP myFileName
    End Sub
        
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
           StopPlay_LOOP ""
    End Sub
    Last edited by 5ms?; Oct 29th, 2009 at 10:24 AM. Reason: Update and Add code

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Looping a .wav File

    Try

    Code:
    Public Sub StopPlay_LOOP()
      sndPlaySound wavFile , SND_NOSTOP 
    End Sub
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Looping a .wav File

    Thanks danasegarane, No!, I Tryd "gust now"
    sndPlaySound wavFile , SND_NOSTOP
    And
    sndPlaySound "" , SND_NOSTOP

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Looping a .wav File

    Here I don't have the movie files.But you are missing the filename in the stop ?
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Looping a .wav File

    Thanks danasegarane, No fix!, wavFile was Global, now it's not.

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Looping a .wav File

    Nice to see it work.

    Refer : How To Play a Waveform (.WAV) Sound File in Visual Basic

    Please mark you thread resolved using the Thread Tools
    Please mark you thread resolved using the Thread Tools as shown

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Looping a .wav File

    It don't work!!
    The thread isn't resolved !!

    SND_LOOP specifies that the sound will continue to play continuously until sndPlaySound is called again with the lpszSoundName$ parameter set to null. You must also specify the SND_ASYNC flag to loop sounds.


    i.e x% = sndPlaySound("",SND_ASYNC)

    But it don't Stop

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Looping a .wav File

    Here is the zip

    Updated the zip.

    Now it works properly.

    Thanks to si_the_geek, for you help.
    Attached Files Attached Files
    Last edited by 5ms?; Nov 3rd, 2009 at 09:54 AM. Reason: Updated the zip

  9. #9
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    Quote Originally Posted by 5ms? View Post
    It don't work
    Yes it works!, it works!, it works!
    Make it, and run the exe, and you will see.

  10. #10
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Looping a .wav File

    You replace the "" in this
    Code:
    Option Explicit
    
      Dim myFileName$
      
     Private Sub Form_Load()
            myFileName = App.Path & "\tmpClick.wav"
            Play_LOOP myFileName
    End Sub
        
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
           StopPlay_LOOP ""
    End Sub
    with myFileName, and put this in a modual

    Private Sub myFileName()
    myFileName = App.Path & "\tmpClick.wav"
    End Sub

    ps. i got it to work fine. thanks.

  11. #11
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    Can some one till me how to stop the loop at Design time

  12. #12
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Looping a .wav File

    wait, what do you mean?

  13. #13
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    Quote Originally Posted by Gamemaster1494 View Post
    wait, what do you mean?


    Make it in to exe (Run time):
    Run the exe, it will Play and Play and Play and ....... ,
    close exe and it'll stop playing.

    programing the project in vb (Design time):
    Run the project, it will Play and Play and Play and ....... ,
    stop project and it will not stop playing,
    close project and it will stop.

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Looping a .wav File

    Design-time only means while you are writing code, etc. Run-time is when you run it, whether that is in VB or compiled.

    The answer to your question is that you cannot stop the code and then have something happen - you need to close it properly (the same way you would when it is compiled), so that the code to tidy up has a chance to run.


    Note that Form_QueryUnload is actually the wrong place for the code, it should be in Form_Unload instead. There is an explanation of why in the article How should I close my form/program/class? from our Classic VB FAQs

  15. #15
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    si_the_geek

    So, if I put the call (StopPlay_LOOP "") in Form_Unload it will Stop Playing the loop when I close the Form?

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Looping a .wav File

    hardLee
    No,

    I ran this, and it's the same.
    Code:
    Option Explicit
    
      Dim myFileName$
      
    Private Sub Form_Load()
            myFileName = App.Path & "\tmpClick.wav"
            Play_LOOP myFileName
    End Sub
        
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
          '
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    StopPlay_LOOP ""
    End Sub


    But when it is compiled (made in to an .exe) it's ok




    .
    Last edited by 5ms?; Nov 2nd, 2009 at 06:40 PM. Reason: But when it is compiled (made in to an .exe) it's ok
    .

    The answer to your question is Here
    And here
    C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98

    Please go to the "Thread Tools" menu at the top of this Thread, and click "Mark Thread Resolved" when you have your answer.
    So I can fine the answer when I need it.

    how to stop the playsound when it is in loop..Play more than 1 sound at a time..
    ini file Check if IP changed Strings 'Split', 'Left' and 'Right'
    Save And Load an Array of list-boxes, to and from a file......list-box and CommonDialog
    Quote Originally Posted by RobDog888

    So please install VB6 service pack 6 as its the latest and last supported service pack MS will ever make.
    http://support.microsoft.com/kb/q198880/ I'm sure this will fix your issue
    The only reason some people get lost in thought is because it’s unfamiliar territory. —Paul Fix

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Looping a .wav File

    It should stop playing if you close the form, but not if you halt the code (using the "Stop" button on the toolbar, the End or Stop commands, etc).

    It does not matter whether it is compiled or not - the only difference that makes is that you can't halt it when it is compiled, so you are forced to close it instead.

  18. #18
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    Quote Originally Posted by si_the_geek View Post
    It should stop playing if you close the form, but not if you halt the code (using the "Stop" button on the toolbar, the End or Stop commands, etc).

    It does not matter whether it is compiled or not - the only difference that makes is that you can't halt it when it is compiled, so you are forced to close it instead.
    si_the_geek

    My brain hurts trying to think how I'm going to fix it.

    My code now
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
          StopPlay_LOOP ""
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    StopPlay_LOOP ""
    End Sub
    I Ran the code it started playing.
    I closed the form (by 'Clicking the close button' on the form)the form closed, I was back to vb Design-time, But it did not stop playing.

    I made it in to an .exe
    Ran the exe and it started playing.
    I closed the exe and it stop playing.

    Quote Originally Posted by 5ms? View Post
    Here is the zip

  19. #19
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Looping a .wav File

    Um, acctualy, i made one that stops by a button. And Yes its in a loop. Ill try to upload it. thats weird.... it wont let me upload it.

  20. #20
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: Looping a .wav File

    Is "it" an exe?, you can not upload an exe.


    exe are ok for me.

    Design-time not ok.
    Last edited by hardLee; Nov 2nd, 2009 at 07:28 PM.

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Looping a .wav File

    I just tried it, and it doesn't stop - and from a quick read of an earlier post, I can see that it is because you have made a clear mistake.

    You got this from the documentation:
    Quote Originally Posted by 5ms? View Post
    SND_LOOP specifies that the sound will continue to play continuously until sndPlaySound is called again with the lpszSoundName$ parameter set to null.
    ..but rather than use a Null string, you used an empty one - and that is completely different (Null means there is no value at all, empty means there is a value with no characters).

    Use this instead:
    Code:
    Public Sub StopPlay_LOOP()
      sndPlaySound vbNullString, 0
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      StopPlay_LOOP
    End Sub
    ...and as I said before, you should not have anything in Form_QueryUnload

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Thumbs up Re: Looping a .wav File

    Thank you si_the_geek.

    Yes, Yes, it works!, it works!,
    One more, Should it be,
    Code:
    sndPlaySound vbNullString, SND_NODEFAULT
    Or
    Code:
    sndPlaySound vbNullString, SND_SYNC
    Attached Images Attached Images  

  23. #23
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Looping a .wav File

    The documentation says to use SND_NODEFAULT, so that would be the best idea.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Thumbs up Re: Looping a .wav File

    Thanks si_the_geek

    Dun that, and Updated the zip

    Quote Originally Posted by 5ms? View Post
    Here is the zip

    Updated the zip.

    Now it works properly.

    Thanks to si_the_geek, for you help.

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