Link to home
Start Free TrialLog in
Avatar of et1dkn
et1dkn

asked on

Playing .wav Files

VB5 Pro...Win98
Would like to play simultaneous .wav files in my app.  Currently using MultiMedia Control..(not locked to it) for single play.  
Avatar of pjknibbs
pjknibbs

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!
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.
ASKER CERTIFIED SOLUTION
Avatar of paulstamp
paulstamp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Global Const WAVEMIX_MAXCHANNELS = 8

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
        Next
        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
    Else
        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

'<<<<<<<<<<<<<<<<Class>>>>>>>
'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
    End If
End Sub
Private Sub Class_Terminate()
    WAVMIX_Close
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()
WavMix.Quit
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"
WavMix.Play
End Sub
Sub Sound3()
WavMix.CurrentChannel = 3
WavMix.FileName = "Explode1.wav"
WavMix.Play
End Sub
Sub Sound4()
WavMix.CurrentChannel = 4
WavMix.FileName = "toon30.wav"
WavMix.Play
End Sub
Sub Sound5()
WavMix.CurrentChannel = 5
WavMix.FileName = "phaser.wav"
WavMix.Play
End Sub
Sub Sound6()
WavMix.CurrentChannel = 6
WavMix.FileName = "playgame.wav"
WavMix.Play
End Sub
Sub Sound7()
WavMix.CurrentChannel = 7
WavMix.FileName = "mgun.wav"
WavMix.Play
End Sub
Sub Sound8()
WavMix.CurrentChannel = 0
WavMix.FileName = "bomb.wav"
WavMix.Play
End Sub
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

Play_Err:
    '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)

BUT IT FREZZES THE HELL OUT OF YOU PTOGRAM
LOL LEARN TO PLAY RES WAVS
Fonature - you're probably right. See new question I've just posted for you.