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

问题描述 投票:0回答:1

这是我第一次寻求帮助。

我已经检查了很多网站,甚至通过维也纳大学的WaybackMachine for Excel文件,也没有找到解决方案。我已经检查了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
excel vba audio wav winmm
1个回答
0
投票

尝试使用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
© www.soinside.com 2019 - 2024. All rights reserved.