Héctor Miguel said:
First, thanks, Hector, for your comments.
But the following does not seem to:
Mute &HAD, 0, 1, 0
sndPlaySound "Default Beep", 0
Mute &HAD, 0, 1, 0
[....]
as Chip told you in your previous post, "sndPlaySound ..., 0"
(or 1) is for sync/async play mode:
I don't see how that relates to my question.
Yes, I am playing the sound synchronously. That is my intent. Since I
associate Windows XP Startup.wav with the "Default Beep" sound, the
sndPlaySound call should take about 4.85 sec. (And it does, as demonstrated
by the macro doit3() below.)
My question is: why don't I see and hear the volume muted for the duration
of the sndPlaySound call, nearly 5 sec?
Let me be clear....
Try each macro below. Each macro gives you the opportunity to mute or
unmute the speaker initially. Normally, I unmute it initially. And
remember to associate Windows XP Startup.wav or the equivalent on your
computer with the "Default Beep" sound.
Why the difference in behavior between doit1() and doit2()?
That is, why does "Mute VK_VOLUME_MUTE,0,1,0" seem to fail to toggle the
volume off, then on around the sndPlaySound call, whereas it does just that
around the Msgbox call?
Moreover, in doit3(), why does the second pair of Mute calls, around the
last Msgbox call, leave the mute state set to the opposite of the initial
mute state?
Caveat: I have executed doit3() dozens of times, and it had consistently
misbehaved per my last question. But in the last 10 executions just now,
doit3() behaved "as expected" 2 or 3 times. That is, it seemingly failed to
toggle mute around the sndPlaySound, a misbehavior that we expect based on
doit2(); but it did correctly toggle mute around the second Msgbox call,
which is only a surprise because of the misbehavior that I have observed
dozens of times.
So the last problem exhibited by doit3() might not be as consistent I first
thought. If you cannot duplicate the misbehaviors, I suggest that you try 2
or 3 more times.
Design notes:
1. Yes, the second pair of Mute calls in doit3() are superfluous, since I
use Msgbox vbInformation, which does not play "Default Beep" sound. I left
them there only because they demonstrate an anomalous behavior, namely my
last question above.
2. I put some delay (Sleep 1000) after each Mute call for two reasons.
First, just in case the Mute operation is asynchronous. (I don't think it
is.) Second, to permit us to see a change of state reflected in the speaker
icon on the desktop toolbar, if you display it. A 1-sec delay is enough
time to accomplish both goals, especially the second one.
Public Declare Sub Sleep Lib "kernel32" (ByVal msec As Long)
Public Declare Function QueryPerformanceFrequency Lib _
"kernel32" (ByRef freq As Currency) As Boolean
Public Declare Function QueryPerformanceCounter Lib _
"kernel32" (ByRef cnt As Currency) As Boolean
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Public Const VK_VOLUME_MUTE = &HAD
Public Declare Sub Mute Lib "user32" Alias "keybd_event" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Sub doit1()
' toggles mute off, then on as expected.
' no sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
MsgBox "press Enter"
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub
Sub doit2()
' seems to fail to toggle mute off, then on.
' produces sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
sndPlaySound "Default Beep", 0
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub
Sub doit3()
Dim sc As Currency, ec As Currency, freq As Currency
' seems to fail to toggle volume off, then on.
' produces sound if speaker unmuted initially
MsgBox "mute or unmute speaker volume", vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
QueryPerformanceCounter sc
sndPlaySound "Default Beep", 0
QueryPerformanceCounter ec
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
' suprise!
' leaves speaker muted if unmuted initially,
' or unmuted if muted initially.
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
QueryPerformanceFrequency freq
MsgBox "sndPlaySound duration: " & _
Format((ec - sc) / freq * 1000, "0 msec"), _
vbInformation
Mute VK_VOLUME_MUTE, 0, 1, 0
Sleep 1000
End Sub
----- original message -----