Discussion:
Playing sound files simultaneously
(too old to reply)
Peter Larsson
2003-10-12 05:59:24 UTC
Permalink
Hi,

I'm trying to use the PlaySound function in VB.NET to play two sound files
simultaneously. We have one "theme song" that is being played throughout the
whole execution of the program, but then we also want to add separate sounds
when certain events occur. For the theme song, we're using the following
class/sub:

Public Class SoundClass
Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name As String,
ByVal hmod As Integer, ByVal flags As Integer) As Integer
Public Const SND_SYNC As Integer = &H0 ' play synchronously
Public Const SND_ASYNC As Integer = &H1 ' play asynchronously
Public Const SND_FILENAME As Integer = &H20000 ' name is file name
Public Const SND_RESOURCE As Integer = &H40004 ' name is resource name
or atom
Public Const SND_LOOP As Integer = &H8 ' Loop sound continuously (used
with SND_ASYNC)
Public Const SND_NOSTOP As Integer = &H10 ' don't stop any currently
playing sound
Public Const SND_NOWAIT As Integer = &H2000 ' don't wait if the driver
is busy

Public Sub PlaySoundFile(ByVal filename As String)
' Plays a sound from filename.
PlaySound(filename, Nothing, SND_FILENAME Or SND_ASYNC Or
SND_LOOP )
End Sub
End Class

And we're invoking the PlaySound sub by:
PlaySound("C:\soundfile.wav")

The question now is how we can make this work with another sound file
simultaneously?

Thank you in advance for any suggestions/solutions!

Peter Larsson
Mike D Sutton
2003-10-12 10:34:20 UTC
Permalink
Post by Peter Larsson
I'm trying to use the PlaySound function in VB.NET to play two sound files
simultaneously. <snip>
Simply call the routine a second time, you may also find the sndPlaySound() API to be helpful. In future though, VB.NET questions
and follow-ups should be posted to .NET specific groups as this is a VB (Classic) group. Have a look here for information on VB.NET
help resources:
Http://www.mvps.org/EDais/DotNet/
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: ***@mvps.org
WWW: Http://www.mvps.org/EDais/
MikeD
2003-10-12 14:06:44 UTC
Permalink
Post by Mike D Sutton
Post by Peter Larsson
I'm trying to use the PlaySound function in VB.NET to play two sound files
simultaneously. <snip>
Simply call the routine a second time, you may also find the
sndPlaySound() API to be helpful. In future though, VB.NET questions
Post by Mike D Sutton
and follow-ups should be posted to .NET specific groups as this is a VB
(Classic) group.

Since we're talking about an API function, it should work the same in VB6 as
it does in VB.NET, but I agree this question would be best asked in a dotnet
newsgroup.

Mike, did you try it yourself? It won't work.

PlaySound and sndPlaySound (the latter of which really shouldn't be used
anymore) are both only capabable of playing one sound at a time within a
process (maybe thread, but I believe it's process). If you check the return
value, you'll notice that for the second sound, it returns 0 if you
specified the SND_NOSTOP flag and stops playing the first sound if you don't
specify that flag.

If you want to play 2 (or more) sounds simultaneously, you must use
something other than PlaySound/sndPlaySound. For example, here's how to do
it with MCI (under VB6; modify accordingly for VB.NET).

Option Explicit

Private Declare Function mciSendString Lib "winmm.dll" Alias
"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As
String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias
"GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As
String, ByVal cchBuffer As Long) As Long

Private Function GetShortFileName(sFileName As String) As String

Dim sBuffer As String
Dim lBufferSize As Long

lBufferSize = 255
sBuffer = String$(lBufferSize, vbNullChar)

GetShortPathName sFileName, sBuffer, lBufferSize

GetShortFileName = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)

End Function

Private Sub Form_Load()

Dim sFile1 As String
Dim sFile2 As String

'Hard-coded for example purposes only
sFile1 = GetShortFileName("C:\OS Shared\MEDIA\Windows Logon Sound.wav")
sFile2 = GetShortFileName("C:\OS Shared\MEDIA\ctmelody.wav")

mciSendString "play " & sFile1, vbNullString, 0&, 0&
mciSendString "play " & sFile2, vbNullString, 0&, 0&

End Sub

Note that with MCI, you should only use the short versions of paths and file
names because spaces within either will screw up MCI. This is because the
MCI command string uses spaces to separate arguments. Enclosing the path
and file name in quotation marks isn't reliable either because the entire
MCI command string is limited to a certain length, which can be easily
exceeded if long names are used.

Mike
Gale Green
2003-10-15 16:09:32 UTC
Permalink
Post by MikeD
Mike, did you try it yourself? It won't work.
I have an app which has the .WAV extension associated with it on my
PC. It plays WAV files using

i = PlaySound( _
FileName, _
0, _
SND_FILENAME Or _
SND_NODEFAULT Or _
SND_ASYNC)

I can click on several WAV files in Windows Explorer, one after the
other, and they will all play concurrently.

Win 98 SE, but maybe the sound card makes a difference.

Gale.
MikeD
2003-10-16 12:01:21 UTC
Permalink
Post by Gale Green
Post by MikeD
Mike, did you try it yourself? It won't work.
I have an app which has the .WAV extension associated with it on my
PC. It plays WAV files using
i = PlaySound( _
FileName, _
0, _
SND_FILENAME Or _
SND_NODEFAULT Or _
SND_ASYNC)
I can click on several WAV files in Windows Explorer, one after the
other, and they will all play concurrently.
Yes, but each time you click a file in Explorer, it probably starts a new
instance of your app. IOW, a new process is started to play the 2nd, 3rd,
etc. .wav files. If you re-read my original reply, I state "are both only
capabable of playing one sound at a time within a process".

Mike

Continue reading on narkive:
Loading...