Results 1 to 3 of 3

Thread: Soundplay repeat problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Unhappy Soundplay repeat problem

    Hi, I have a form with a command button which plays a 4 second wav file using the PlaySound function (winnm.dll) which is set to play SND_SYNC (play then return to code) Everything works as it should however if I continue to click on the button as the wav file is playing it somehow remembers the clicks and then plays the wav again. Is there some way to prevent this? Please save my sanity and help. I have tried disabling the button and even making it invisable but "AAAAAAAAGH!" it still remembers the clicks during the 4 seconds.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Soundplay repeat problem

    The clicks are being added to your project's message queue. The answer is to prevent them from getting to the queue or remove them from the queue after sound plays. Without advanced code, preventing them from the queue can be done like so:

    You can disable your button before playing the sound then re-enable it after it is done playing

    Edited. Sounded logical but it didn't work. Maybe PeekMessage API? Thinking....

    Yep PeekMessage appears to be a good choice, easier than subclassing to intercept the clicks:
    Code:
    Private Declare Function PeekMessage Lib "user32.dll" Alias "PeekMessageA" (ByRef lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Type MSG
        hWnd As Long
        message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    Private Const PM_REMOVE As Long = &H1
    Private Const WM_LBUTTONDOWN As Long = &H201
    Private Const WM_LBUTTONUP As Long = &H202
    
    Private Sub Command1_Click()
       Dim pMsg As MSG
    
        ' ... your code
    
        ' clear the queue of clicks
            Do Until PeekMessage(pMsg, Command1.hWnd, WM_LBUTTONDOWN, WM_LBUTTONUP, PM_REMOVE) = 0&
            Loop
    End Sub
    Change Command1 in above code to reflect your button's name & index as needed. You may want to read more about that API (linked above). Though this fixes the mouse clicks, it does not deal with user hitting space bar or enter while button has focus. A bit of an exercise I'll leave to you for now.
    Last edited by LaVolpe; Jul 15th, 2011 at 09:06 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Smile Re: Soundplay repeat problem

    Thanks for the quick response. Works great. I will work on solving the key problem. Thanks again.

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