How do I stop a PlaySound call that continues output outside the logic of the program that called it?

spc909
spc909 used Ask the Experts™
on
Below code executes correctly up to the 1st instance that should produce sound.  That is the 12th bar of the 5th cycle.  Problem is that once the sound is called the 1st time . . . it continues to sound every bar thereafter, even though the code shows it is clearly a one time event.

For C = 1 To CycPhase
        iCycStart = (AncDyBar + 1)
        iCycEnd = (AncDyBar + (CycLength - 1))      
       pBarCnt = 0
        '*********** Next I Sequence **************
        For i = iCycStart To iCycEnd
            pBarCnt = pBarCnt + 1
            '        PlayNow = PlaySound(Null, 0, 0)  'Commented out because does not work
            If pBarCnt = 12 Then
                v1(z1, i) = Bar(2, i)
                If C = 5 Then     '5th cycle
                    PlayNow = PlaySound(A, p, SND_FILENAME Or SND_ASYNC Or SND_NOWAIT)
                End If
            End If
        Next i
        AncDyBar = i
    Next C

I've tried SndPurge and the Null string as shown commented out above.  The null string stops the dll from working  altogether and the SndPurge does nothing . . . and nothing stops a repeating call to the wav file.  Below is further information to help.

Public Declare Function PlaySound _
               Lib "winmm.dll" _
               Alias "PlaySoundA" (ByVal lpszName As String, _
                                   ByVal hModule As Long, _
                                   ByVal dwFlags As Long) As Long
Public Const SND_FILENAME = &H20000     'name is a file name
Public Const SND_ASYNC = &H1                 'play asynchronously
Public Const SND_NOWAIT = &H2000          'don't wait if the driver is busy
Public Const SND_PURGE = &H40                'stop sound (longer wav)

Thank You,
spc909
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
try

PlaySound(Nothing, 0, 0)

Author

Commented:
Morning EDDYKT,

PlaySound(Nothing, 0, 0)
gives . . . Compile error:
Invalid use of object
Previously I have also tried PlaySound("", 0, 0)

Other ideas?
spc909
Commented:
how about

PlaySound(vbNullString, 0, 0)

Author

Commented:
Wow, I'd begun to think it wasn't possible.
Thank you.

That definitely works, however . . . as I scroll through the bars I hear a click kind of sound every so often . . . which implies the something is still trying to play but is then stopped perhaps.  So can you suggest how to stop that  . . . better code structure perhaps?  I don't know.
Here's how it looks now and works . . . maybe not perfect.

For i = iCycStart To z
            pBarCnt = pBarCnt + 1
            PlayNow = PlaySound(vbNullString, 0, 0)
            If pBarCnt = 12 Then
                v1(z1, i) = Bar(2, i)
                If C = 5 Then     '5th cycle
                    PlayNow = PlaySound(A, p, SND_FILENAME Or SND_ASYNC Or SND_NOWAIT)
                    ' This should only play on the 12th bar of the 5th cycle
                    ' now it plays correctly with the above vbNullString, but there is
                    ' apparently random clicking sound played
                    ' intermittently going forward from 1st triggering of event
                End If
            End If
 Next i
 
Thanks so much,
spc909
Commented:
may be you should not call

PlaySound(vbNullString, 0, 0)
on every loop

use a flag to indicate the play and stop status

Author

Commented:
Thanks again EDDYKT,

For the record the below works perfect.

For C = 1 To CycPhase
        iCycStart = (AncDyBar + 1)
        iCycEnd = (AncDyBar + (CycLength - 1))
        If iCycEnd > LastBar Then
            z = LastBar          'displays future geometry in Own period
            Else: z = iCycEnd
        End If
        PlayStop = 0
        pBarCnt = 0
        '******************************************** Next I Sequence ***********************************************
        For i = iCycStart To z
           If PlayStop = 1 Then
               PlayNow = PlaySound(vbNullString, 0, 0)  'Better location perhaps
           End If
            pBarCnt = pBarCnt + 1
            If pBarCnt = 12 Then
                v1(z1, i) = Bar(2, i)
                If C = 4 Then     '5th cycle
                    PlayNow = PlaySound(A, p, SND_FILENAME Or SND_ASYNC Or SND_NOWAIT)
                    PlayStop = 1
                End If
            End If
        Next i
        AncDyBar = i
 Next C

Such great help here!

Best,
spc909

Author

Commented:
Perfect

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial