Solved

Excel VBA Play multiple sounds at once

Posted on 2014-02-01
6
946 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EXCEL 2010 7 65
IT Desktop Support 11 65
SharePoint 2013 Link to File on Network Drive Not Working 3 20
say, Publisher - saving as Print Ready pdf 3 20
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 …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

816 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

12 Experts available now in Live!

Get 1:1 Help Now