Solved

Excel VBA Play multiple sounds at once

Posted on 2014-02-01
6
1,011 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 IT Pros

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

734 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