Solved

Playing .wav Files

Posted on 2000-03-12
7
649 Views
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.  
0
Comment
Question by:et1dkn
7 Comments
 
LVL 12

Expert Comment

by:pjknibbs
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!
0
 

Expert Comment

by:hess
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.
0
 
LVL 3

Accepted Solution

by:
paulstamp earned 100 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
      sound.


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

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

Hope this helps.

0
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.

 

Expert Comment

by:FONATURE
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

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
0
 

Expert Comment

by:FONATURE
ID: 2610653
0
 

Expert Comment

by:FONATURE
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

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
0
 
LVL 3

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 42
Determine Range to Select 5 53
VBA Shell can't Find Word document 11 112
Help me. 3 60
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

827 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