Solved

Excel VBA Play multiple sounds at once

Posted on 2014-02-01
6
1,030 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
[X]
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
  • 3
  • 3
6 Comments
 
LVL 27

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 27

Expert Comment

by:MacroShadow
ID: 39828393
The alias name is the complete file name (including path). Works in VBA too.
0
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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 27

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

687 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