HOW TO PLAY SOUND.WAV FILES SIMULTANEOUSLY UNDER EXCEL

H

Hotbird

I have a requirement to trigger 3 sound effects during execution of an Excel
macro. When the Workbook opens, Function Keys are assigned to each Sound
procedure as follows:

Option Explicit
Private Sub Workbook_Open()

' ASSIGN FUNCTION KEYS

Application.OnKey "{F1}", "Sound1"
Application.OnKey "{F2}", "Sound2"
Application.OnKey "{F3}", "Sound3"

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)

' RESET FUNCTION KEYS

Application.OnKey "{F1}"
Application.OnKey "{F2}"
Application.OnKey "{F3}"

End Sub

The following Windows API function declarations appear in the Module code:

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

Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000

The individual Sound procedures are:

Public Sub Sound1()

Call PlaySound(ThisWorkbook.Path & "\sound1.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound2()

Call PlaySound(ThisWorkbook.Path & "\sound2.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound3()

Call PlaySound(ThisWorkbook.Path & "\sound3.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub

When F1 is pressed, sound1.wav file plays, and similarly when F2 and F3 are
pressed, the corresponding sound2.wav and sound3.wav files play. So far, so
good. If a second sound file is triggered before the first one has finished
playing, the first one is abandoned, and the second plays alone. What I want
is to allow more than one sound file to be triggered and play to completion
without being interrupted.

Is there way of achieving this, perhaps involving a different Windows API
function declaration? Or would 3 Sound Cards be needed to play the wav files
completely independently?
 
J

Jake Marx

Hi Hotbird,

PlaySound will not play WAV files simultaneously. As you've discovered,
when you start a new sound, any current sounds will stop.

It looks like you may have to use the mcisendstring API call to do this. Do
a search in google, and you'll get lots of results back.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
H

Hotbird

Thanks for this explanation. I have searched Google for MCISendString, and
found examples which allow a WAIT parameter to be specified which ensures
that a playing file is not interrupted. So far, however, I have not seen any
references to multiple wav or midi files playing simultaneously and
independently. Has anyone experience of doing this?

Jake Marx said:
Hi Hotbird,

PlaySound will not play WAV files simultaneously. As you've discovered,
when you start a new sound, any current sounds will stop.

It looks like you may have to use the mcisendstring API call to do this. Do
a search in google, and you'll get lots of results back.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

I have a requirement to trigger 3 sound effects during execution of
an Excel macro. When the Workbook opens, Function Keys are assigned
to each Sound procedure as follows:

Option Explicit
Private Sub Workbook_Open()

' ASSIGN FUNCTION KEYS

Application.OnKey "{F1}", "Sound1"
Application.OnKey "{F2}", "Sound2"
Application.OnKey "{F3}", "Sound3"

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)

' RESET FUNCTION KEYS

Application.OnKey "{F1}"
Application.OnKey "{F2}"
Application.OnKey "{F3}"

End Sub

The following Windows API function declarations appear in the Module
code:

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

Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000

The individual Sound procedures are:

Public Sub Sound1()

Call PlaySound(ThisWorkbook.Path & "\sound1.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound2()

Call PlaySound(ThisWorkbook.Path & "\sound2.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound3()

Call PlaySound(ThisWorkbook.Path & "\sound3.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub

When F1 is pressed, sound1.wav file plays, and similarly when F2 and
F3 are pressed, the corresponding sound2.wav and sound3.wav files
play. So far, so good. If a second sound file is triggered before
the first one has finished playing, the first one is abandoned, and
the second plays alone. What I want is to allow more than one sound
file to be triggered and play to completion without being interrupted.

Is there way of achieving this, perhaps involving a different Windows
API function declaration? Or would 3 Sound Cards be needed to play
the wav files completely independently?
 
J

Jake Marx

Hmmm...I've never done this, so maybe MCISendString is not the way to go.

The DirectX platform would be much more robust, but it's a lot more complex
than PlaySound.

Here's a link to get you started:

http://216.5.163.53/DirectX4VB/Tutorials/DirectX8/DA_SoundFx.asp

You would have to set a reference in your project to the DirectX 8.0
library.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

Thanks for this explanation. I have searched Google for
MCISendString, and found examples which allow a WAIT parameter to be
specified which ensures that a playing file is not interrupted. So
far, however, I have not seen any references to multiple wav or midi
files playing simultaneously and independently. Has anyone
experience of doing this?

Jake Marx said:
Hi Hotbird,

PlaySound will not play WAV files simultaneously. As you've
discovered, when you start a new sound, any current sounds will stop.

It looks like you may have to use the mcisendstring API call to do
this. Do a search in google, and you'll get lots of results back.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

I have a requirement to trigger 3 sound effects during execution of
an Excel macro. When the Workbook opens, Function Keys are assigned
to each Sound procedure as follows:

Option Explicit
Private Sub Workbook_Open()

' ASSIGN FUNCTION KEYS

Application.OnKey "{F1}", "Sound1"
Application.OnKey "{F2}", "Sound2"
Application.OnKey "{F3}", "Sound3"

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)

' RESET FUNCTION KEYS

Application.OnKey "{F1}"
Application.OnKey "{F2}"
Application.OnKey "{F3}"

End Sub

The following Windows API function declarations appear in the Module
code:

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

Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000

The individual Sound procedures are:

Public Sub Sound1()

Call PlaySound(ThisWorkbook.Path & "\sound1.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound2()

Call PlaySound(ThisWorkbook.Path & "\sound2.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub
Private Sub Sound3()

Call PlaySound(ThisWorkbook.Path & "\sound3.wav", 0&, SND_ASYNC Or
SND_FILENAME)

End Sub

When F1 is pressed, sound1.wav file plays, and similarly when F2 and
F3 are pressed, the corresponding sound2.wav and sound3.wav files
play. So far, so good. If a second sound file is triggered before
the first one has finished playing, the first one is abandoned, and
the second plays alone. What I want is to allow more than one sound
file to be triggered and play to completion without being
interrupted.

Is there way of achieving this, perhaps involving a different
Windows API function declaration? Or would 3 Sound Cards be needed
to play the wav files completely independently?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top