Synchronize sound w/VB6 form

Posted on 2007-10-05
Last Modified: 2013-12-26
I'm working in VB6.  I need to synchronize the playing of a sound with a condition on a form.  The condition is whether or not an image is visible .  The problem is this;  When I evaluate for the condition, it's being done on one of the form's 'prep' events (load, initialize, paint, etc - I've tried lots).  Thus, the sound plays before the form is fully displayed.  Is there a way the form can completely display and then raise an event which evaluates the image (visible or not) and THEN plays the sound?
Question by:wjshore
    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    Ive used a single timer control to delay reprocessing a form (after user selection) until it has finished displaying.

    it's interval is set to 50 milliseconds.

    Private Sub cboHighlight_check()
        'routine to detect change of phase
        If cboHighlight.ListIndex <> mHighlightId Then
            mHighlightId = cboHighlight.ListIndex
            tmrPhaseChange.Enabled = True
        End If
    End Sub

    the timer then fires and form contents get redrawn.
    LVL 10

    Accepted Solution

    Probably the best way to avoid modal/modeless problems with showing the form is to use an async playing for the sound i.e., play the sound AND continue loading/displaying the form at the same time.  If there's not a bucket load of stuff done in the form, it should appear more or less at the same instant as the sound does.

    Use PlaySound with SND_SYNC

    Public Const SND_SYNC = &H0          '  Play synchronously (default).
    Public Const SND_ASYNC = &H1         '  Play asynchronously.

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

    Call PlaySound(Sound, App.hInstance, SND_ASYNC)
    LVL 44

    Expert Comment

    put your code in the Activate event.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    "Is there a way the form can completely display..."

    Here is how to make the form show itself before continuing with the rest of the Load event:

    Private Sub Form_Load()
        ' force the form to show itself
        ' continue doing other stuff...
    End Sub
    LVL 10

    Expert Comment

    >>"Here is how to make the form show itself before continuing with the rest of the Load event:"

    As long as it's not been shown modally elsewhere, e.g., that will fail if elsewhere *this* form's being implicitly loaded via, say, someForm.Show vbModal

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now