Playing .wav Files

Posted on 2000-03-12
Medium Priority
Last Modified: 2008-03-17
VB5 Pro...Win98
Would like to play simultaneous .wav files in my app.  Currently using MultiMedia Control..(not locked to it) for single play.  
Question by:et1dkn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 2610077
The only way to play multiple sounds at the same time in Windows is currently to use DirectSound, which is part of the DirectX multimedia set. I believe the Windows Media Player control uses this, so if you put two of those on a form you may be able to play two sounds simultaneously. I haven't tried this, though!

Expert Comment

ID: 2610311
um... there is an api that plays wave files and all you have to pass to it is a path to the file. this might do what you want and is much more light weight than the MM control. just jump on altavista and search for +wav +api +visual +basic and you'll find what you need.

Accepted Solution

paulstamp earned 400 total points
ID: 2610388
The API you require is :

Declare Function sndPlaySound Lib "WINMM.DLL" Alias "sndPlaySoundA" _
      (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

The flags parameter can be one or more of the following :

- SND_SYNC specifies that the sound is played synchronously and the
      function does not return until the sound ends.

    - SND_ASYNC specifies that the sound is played asynchronously and the
      function returns immediately after beginning the sound.

    - SND_NODEFAULT specifies that if the sound cannot be found, the
      function returns silently without playing the default sound.

    - SND_LOOP specifies that the sound will continue to play continuously
      until sndPlaySound is called again with the lpszSoundName$ parameter
      set to null. You must also specify the SND_ASYNC flag to loop sounds.

    - SND_NOSTOP specifies that if a sound is currently playing, the
      function will immediately return False without playing the requested

The following example starts to play a sound and returns for the next sound :

 SoundName$ = "c:\firstsound.wav"
   x% = sndPlaySound(SoundName$,wFlags%)
SoundName$ = "c:\nextsound.wav"
   x% = sndPlaySound(SoundName$,wFlags%)

Hope this helps.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 2610631
You Can Play Up to 8 wavs at the same time with wavmix32.dll
you need wavmix32.dll for this to work
Ok in a Modual
Option Explicit
Global hMixSession As Long
Global lpWaveMix() As Long
Global WaveHandle As Long
Global WAVMIX_Quiet As Integer


Type tChannelInfo
    Loops As Long
    WaveFile As String
End Type

Type tWaveMixInfo
   wSize As Integer
   bVersionMajor As String * 1
   bVersionMinor As String * 1
   szDate(12) As String
   dwFormats As Long
End Type

Type tMixConfig
    wSize As Integer
    dwFlagsLo As Integer
    dwFlagsHi As Integer
    wChannels As Integer
    wSamplingRate As Integer
End Type

Private Type tMixPlayParams
    wSize         As Integer
    hMixSessionLo As Integer
    hMixSessionHi As Integer
    iChannelLo    As Integer
    iChannelHi    As Integer
    lpMixWaveLo   As Integer
    lpMixWaveHi   As Integer
    hWndNotifyLo  As Integer
    hWndNotifyHi  As Integer
    dwFlagsLo     As Integer
    dwFlagsHi     As Integer
    wLoops        As Integer
End Type

Declare Function WaveMixInit Lib "WAVMIX32.DLL" () As Long
Declare Function WaveMixConfigureInit Lib "WAVMIX32.DLL" _
        (lpConfig As tMixConfig) As Long
Declare Function WaveMixActivate Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal fActivate As Integer) As Long
Declare Function WaveMixOpenWave Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal szWaveFilename As String, _
        ByVal hInst As Long, ByVal dwFlags As Long) As Long
Declare Function WaveMixOpenChannel Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal iChannel As Long, _
        ByVal dwFlags As Long) As Long
Declare Function WaveMixPlay Lib "WAVMIX32.DLL" (lpMixPlayParams As Any) As Integer
Declare Function WaveMixFlushChannel Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal iChannel As Integer, _
        ByVal dwFlags As Long) As Integer
Declare Function WaveMixCloseChannel Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal iChannel As Integer, _
        ByVal dwFlags As Long) As Integer
Declare Function WaveMixFreeWave Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long, ByVal lpMixWave As Long) As Integer
Declare Function WaveMixCloseSession Lib "WAVMIX32.DLL" _
        (ByVal hMixSession As Long) As Integer
Declare Sub WaveMixPump Lib "WAVMIX32.DLL" ()
Declare Function WaveMixGetInfo Lib "WAVMIX32.DLL" _
        (lpWaveMixInfo As tWaveMixInfo) As Integer
Private Function HiWord(ByVal l As Long) As Integer
    l = l \ &H10000
    HiWord = Val("&H" & Hex$(l))
End Function

Private Function LoWord(ByVal l As Long) As Integer
    l = l And &HFFFF&
    LoWord = Val("&H" & Hex$(l))
End Function

Sub WAVMIX_SetFile(FileName As String, AChannel As Long)
Dim rc As Long
    If WAVMIX_Quiet Then Exit Sub
    If AChannel > UBound(lpWaveMix) Then
        ReDim Preserve lpWaveMix(AChannel)
        WaveHandle = AChannel
    End If
    If lpWaveMix(AChannel) <> 0 Then
        WAVMIX_StopChannel AChannel
        rc = WaveMixFreeWave(hMixSession, lpWaveMix(AChannel))
    End If
    ' Open the new wave and assign it to this channel.
    lpWaveMix(AChannel) = WaveMixOpenWave(hMixSession, FileName, 0, 0)
    rc = WaveMixOpenChannel(hMixSession, AChannel, 0)
End Sub
Sub WAVMIX_Close()
Dim rc As Long
Dim i As Integer
    If WAVMIX_Quiet Then Exit Sub

    If (hMixSession <> 0) Then
        For i = 0 To UBound(lpWaveMix)
            If lpWaveMix(i) <> 0 Then
                WAVMIX_StopChannel CLng(i)
                rc = WaveMixFreeWave(hMixSession, lpWaveMix(i))
            End If
        rc = WaveMixCloseSession(hMixSession)
        hMixSession = 0
    End If
End Sub

Function WAVMIX_InitMixer() As Integer
' Initialize and activate the WaveMix DLL.
Dim rc As Long
Dim config As tMixConfig

    If WAVMIX_Quiet Then Exit Function

    WaveHandle = 0
    ReDim lpWaveMix(0)
    ChDir App.Path
    config.wSize = Len(config)
    config.dwFlagsHi = 1
    config.dwFlagsLo = 0
    'Allow stereo sound
    config.wChannels = 2
    hMixSession = WaveMixConfigureInit(config)
    rc = WaveMixActivate(hMixSession, True)

    If (rc <> 0) Then
        WAVMIX_InitMixer = False
        Call WaveMixCloseSession(hMixSession)
        hMixSession = 0
        WAVMIX_InitMixer = True
    End If
End Function

Sub WAVMIX_StopChannel(ByVal ChannelNum As Long)
' Stop playing the specified channel.
Dim rc As Integer

    If WAVMIX_Quiet Then Exit Sub
    If (hMixSession = 0) Then Exit Sub
    rc = WaveMixFlushChannel(hMixSession, ChannelNum, 0)
End Sub

Sub WAVMIX_Activate(Activate As Long)
' Activate the WaveMix DLL.
Dim rc As Integer

    If WAVMIX_Quiet Then Exit Sub
    If (hMixSession = 0) Then Exit Sub

    rc = WaveMixActivate(hMixSession, Activate)
End Sub

Sub WAVMIX_PlayChannel(ChannelNum As Long, LoopWave As Long)
' Play a specified channel, and indicate whether the sound
' should be looped.
Dim params As tMixPlayParams
Dim rc As Long

    If WAVMIX_Quiet Then Exit Sub
    If ChannelNum > UBound(lpWaveMix) Then Exit Sub
    If (hMixSession = 0) Then Exit Sub

    params.wSize = Len(params)
    params.hMixSessionLo = LoWord(hMixSession)
    params.hMixSessionHi = HiWord(hMixSession)
    params.iChannelLo = LoWord(ChannelNum)
    params.iChannelHi = HiWord(ChannelNum)
    params.lpMixWaveLo = LoWord(lpWaveMix(ChannelNum))
    params.lpMixWaveHi = HiWord(lpWaveMix(ChannelNum))
    params.hWndNotifyLo = 0
    params.hWndNotifyHi = 0
    params.dwFlagsHi = 5
    params.dwFlagsLo = 0
    params.wLoops = LoopWave
    rc = WaveMixPlay(params)
End Sub

'in a Class Modual
Option Explicit
Dim Channel(0 To 7) As tChannelInfo
Public CurrentChannel As Long
Private Sub Class_Initialize()
    If Not WAVMIX_InitMixer() Then
        MsgBox "Unable to Initialize WaveMix DLL", _
               vbOKOnly Or vbExclamation, _
               "WaveMix Error"
    End If
End Sub
Private Sub Class_Terminate()
End Sub
Public Sub Play()
    WAVMIX_PlayChannel CurrentChannel, Channel(CurrentChannel).Loops
End Sub
Public Property Get FileName() As String
    FileName = Channel(CurrentChannel).WaveFile
End Property
Public Property Let FileName(ByVal AFileName As String)
    Channel(CurrentChannel).WaveFile = AFileName
    WAVMIX_SetFile Channel(CurrentChannel).WaveFile, CurrentChannel
End Property
Public Sub Quit()
    WAVMIX_StopChannel CurrentChannel
End Sub
Public Property Get AutoLoop()
    AutoLoop = Channel(CurrentChannel).Loops
End Property
Public Property Let AutoLoop(LoopWave)
    Channel(CurrentChannel).Loops = LoopWave
End Property

'In the form with 8 command buttons
Option Explicit
Dim WavMix As cWaveMix

Private Sub Command1_Click()
Call Sound1
End Sub

Private Sub Command2_Click()
Call Sound2
End Sub

Private Sub Command3_Click()
Call Sound3
End Sub

Private Sub Command4_Click()
Call Sound4
End Sub

Private Sub Command5_Click()
Call Sound5
End Sub

Private Sub Command6_Click()
Call Sound6
End Sub

Private Sub Command7_Click()
Call Sound7
End Sub

Private Sub Command8_Click()
Call Sound8
End Sub

Private Sub Command9_Click()
End Sub

Private Sub Form_Load()
    Set WavMix = New cWaveMix
End Sub
Sub Sound1()
WavMix.CurrentChannel = 1             'Heres the channel number you can play up to 8 channels at the same time
WavMix.FileName = "laser2.wav"       WavMix.Play                          
WavMix.Play                           'Play the wav
End If
End Sub
Sub Sound2()
WavMix.CurrentChannel = 2
WavMix.FileName = "toon31.wav"
End Sub
Sub Sound3()
WavMix.CurrentChannel = 3
WavMix.FileName = "Explode1.wav"
End Sub
Sub Sound4()
WavMix.CurrentChannel = 4
WavMix.FileName = "toon30.wav"
End Sub
Sub Sound5()
WavMix.CurrentChannel = 5
WavMix.FileName = "phaser.wav"
End Sub
Sub Sound6()
WavMix.CurrentChannel = 6
WavMix.FileName = "playgame.wav"
End Sub
Sub Sound7()
WavMix.CurrentChannel = 7
WavMix.FileName = "mgun.wav"
End Sub
Sub Sound8()
WavMix.CurrentChannel = 0
WavMix.FileName = "bomb.wav"
End Sub

Expert Comment

ID: 2610653

Expert Comment

ID: 2618888
If you wanted to play one wav after another then ask for that. The answer only does that.I did a multipul wav play

to play a wav in a modual

#If Win32 Then
    Declare Function sndPlaySound Lib "winmm" Alias "sndPlaySoundA" _
    (ByVal lpSound As String, ByVal flag As Long) As Long
#ElseIf Win16 Then
    Declare Function sndPlaySound Lib "MMSYSTEM" _
    (ByVal lpszSoundName As String, ByVal uFlags As Integer) As Integer
#End If

Sub PlayWave(sFileName As String)
    On Error GoTo Play_Err
    Dim iReturn As Integer
    'Make sure something was passed to the Play Function
    If sFileName > "" Then
        'Make sure a WAV filename was passed
        If UCase$(Right$(sFileName, 3)) = "WAV" Then
            'Make sure the file exists
            If Dir(sFileName) > "" Then
                iReturn = sndPlaySound(sFileName, 0)
            End If
        End If
    End If
    'Wav file play successful
    Exit Sub

    'If there was an error then exit without playing
    Exit Sub
End Sub

to call it

Playwave (1.wav)
Playwave (2.wav)
Playwave (3.wav)


Expert Comment

ID: 2618904
Fonature - you're probably right. See new question I've just posted for you.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

770 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