如何通过VBA同时异步循环播放Excel中的多个WAV文件?

How to play multiple WAV files in Excel by VBA asynchronously looped at the same time?

第一次求助

我已经检查了很多网站,甚至通过维也纳大学的 Excel 文件的 WaybackMachine 也没有找到解决方案。我检查了 winmm.dll API 中的函数,例如 sndPlaySoundA 和 mciSendStringA。

我可以异步循环播放 WAV 文件,但目前无法同时异步循环播放另一个文件。正如您在下面看到的,可以组合启用循环和异步的标志。我试过将它与其他标志结合使用,但没有我想要的结果。

Private Declare Function PlaySound Lib "winmm" Alias "sndPlaySoundA" ( _
    ByVal lpszSoundName As String, _
    ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1
Private Const SND_LOOP = &H8
Private Const SND_NODEFAULT = &H2

Private Sub LoopTrack(ByVal sTrack As String)
    Dim lReturnValue As Long
    lReturnValue = PlaySound(sTrack, SND_ASYNC Or SND_LOOP)
    If lReturnValue = 0 Then MsgBox "Can't play " & sTrack
End Sub

Public Sub PlayBackStop()
    Call PlayTrack(vbNullString)
End Sub

Private Sub PlayTrack(ByVal sTrack As String)
    Dim lReturnValue As Long
    lReturnValue = PlaySound(sTrack, SND_ASYNC Or SND_NODEFAULT)
    If lReturnValue = 0 Then MsgBox "Can't play " & sTrack
End Sub

我也试过下面的代码异步播放多个文件但不循环播放。知道如何异步循环多个文件并控制它们吗?

Private Declare Function SendString Lib "winmm" Alias "mciSendStringA" ( _
    ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Long, _
    ByVal hwndCallback As Long) As Long

Public Sub PlayMultimedia( _
    ByVal sAliasName As String, _
    Optional ByVal sFirstFrame As String = vbNullString, _
    Optional ByVal sLastFrame As String = vbNullString)

    If sFirstFrame = vbNullString Then sFirstFrame = 0
    If sLastFrame = vbNullString Then sLastFrame = GetTotalFrames(sAliasName)

    Dim sToDo As String * 128
    Dim lReturnValue As Long
    Dim sErrorToReturn As String * 128

    sToDo = "play " & sAliasName & " from " & sFirstFrame & " to " & sLastFrame

    lReturnValue = SendString(sToDo, 0&, 0&, 0&)
    If Not lReturnValue = 0 Then
        GetError lReturnValue, sErrorToReturn, 128
        MsgBox sErrorToReturn
        End
    End If
End Sub

Public Function GetTotalFrames(ByVal sAliasX As String) As Long
    Dim lReturnValue As Long
    Dim sTotalFrames As String * 128

    lReturnValue = SendString("set " & sAliasX & " time format frames", sTotalFrames, 128, 0&)

    lReturnValue = SendString("status " & sAliasX & " length", sTotalFrames, 128, 0&)
    If Not lReturnValue = 0 Then
        GetTotalFrames = -1
        Exit Function
    End If

    GetTotalFrames = Val(sTotalFrames)
End Function

尝试使用WMPlayer.OCX,示例如下:

Dim wmp1
Dim wmp2

Sub test()

    Set wmp1 = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    With wmp1
        .url = "C:\Test\sound1.wav"
        .controls.play
        .settings.playCount = 5
    End With
    Set wmp2 = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    With wmp2
        .url = "C:\Test\sound2.mp3"
        .controls.play
        .settings.setMode "loop", True
    End With

End Sub

顺便说一句,Set wmp1 = CreateObject("WMPlayer.OCX")Set wmp1 = New WMPLib.WindowsMediaPlayer 参考 C:\Windows\System32\wmp.dll 对我不起作用,这就是我使用 new: 绰号的原因。