Solved

Excel VBA Play multiple sounds at once

Posted on 2014-02-01
6
900 Views
Last Modified: 2014-02-02
I'd like to initiate sound files from VBA so that the first sound plays while the second sound also plays.  In fact, I'd like to have multiple sounds playing at the same time, to stop under program control.  The winmm.dll sndPlaySound32 function works to play one sound, but no matter the flags I send, a second sound call stops the first sound.  Suggestions how best to accomplish what I want?
0
Comment
Question by:RolandGarton
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39827496
This is a small part of a larger VB6 project to deal with multimedia files (source: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9783&lngWId=1)

Option Explicit

'Private Declares
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 mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long

'variable just in the module
Dim glo_from As Long
Dim glo_to As Long
Dim glo_AliasName As String
Dim glo_hWnd As Long

Public Function PlayMultimedia(AliasName As String, from_where As String, to_where As String) As String
'Calling PlayMultimedia will playing the multimedia file.
'Parameters

'AliasName
'[in]Specifies name alias name which you want play it
'Note : you must let this parameter the alias which you
'used it OpenMultimedia Function or this function not Success

'from_where
'[in] Specifies the first frame in playing

'to_where
'[in]Specifies the last frame in playing

'if from_where is vbNullString and the to_where is vbNullString the Function will:
'playing from the beginning to end.

'if from_where is 10 and to_where is 100 the Function will:
'playing from 10 to 100 and stop.

'if from_where is vbNullString and to_where is 100 the Function will:
'playing from the beginning to 100 and stop.

'if from_where is 104 and to_where is vbNullString the Function will:
'playing from 104 to end.

'Note :the numbers 10,100,104 is an example for from where start playing to where end playing

'Note : if this Function success will return value string "Success"
'or if not will return value string description the error which occur

    If from_where = vbNullString Then from_where = 0
    If to_where = vbNullString Then to_where = GetTotalframes(AliasName)

    'Improtant for auto repeat
    If AliasName = glo_AliasName Then
        glo_from = from_where
        glo_to = to_where
    End If

    Dim cmdToDo As String * 128
    Dim dwReturn As Long
    Dim ret As String * 128

    cmdToDo = "play " & AliasName & " from " & from_where & " to " & to_where

    dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&)    'play

    If Not dwReturn = 0 Then  'not success
        mciGetErrorString dwReturn, ret, 128    'get the error
        PlayMultimedia = ret
        Exit Function
    End If

    'Success
    PlayMultimedia = "Success"
End Function

Public Function GetTotalframes(AliasName As String) As Long
'Calling GetTotalframes will Get the Total frames for
'the multimedia file

'Parameters

'AliasName
'[in]Specifies name alias name which you want Get Total frames for it
'Note : you must let this parameter the alias which you
'used it OpenMultimedia Function or this function not Success

'Note : if this Function success will return value long
'is "number of total frames"
'or if not will return value long is -1

    Dim dwReturn As Long
    Dim Total As String * 128

    dwReturn = mciSendString("set " & AliasName & " time format frames", Total, 128, 0&)
    dwReturn = mciSendString("status " & AliasName & " length", Total, 128, 0&)

    If Not dwReturn = 0 Then  'not success
        GetTotalframes = -1
        Exit Function
    End If

    'Success
    GetTotalframes = Val(Total)
End Function

Open in new window

0
 

Author Comment

by:RolandGarton
ID: 39828330
I want to play sound files, either .WAV or .MP3.  How can I find an alias name for the file? The comments say:

'AliasName
'[in]Specifies name alias name which you want Get Total frames for it
'Note : you must let this parameter the alias which you
'used it OpenMultimedia Function or this function not Success

...but there is no reference to any OpenMultimedia function in the code.

This is VB6 code.  Will it work in Visual Basic for Applications?  (I'm implementing this in Excel.)
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39828393
The alias name is the complete file name (including path). Works in VBA too.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:RolandGarton
ID: 39828487
VBA on my machine does not recognize any OpenMultimedia function.  Do I need to do some configuration?
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39828502
You don't need it, ignore the comments they are part of a more sophisticated program.

Copy the code to a new module and call it like this:
Sub Demo()
    Dim strResult As String
    strResult = PlayMultimedia("C:\Demo\1.WAV", vbNullString, vbNullString)
    strResult = PlayMultimedia("C:\Demo\2.WAV", vbNullString, vbNullString)
    strResult = PlayMultimedia("C:\Demo\3.WAV", vbNullString, vbNullString)
End Sub

Open in new window

0
 

Author Closing Comment

by:RolandGarton
ID: 39828720
Great...that does the trick...thanks much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now