Solved

Excel VBA Play multiple sounds at once

Posted on 2014-02-01
6
929 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 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

932 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