gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:02:28 29 4
gpt4 key购买 nike

这是我第一次寻求帮助。

我已经检查了很多网站,甚至通过维也纳大学的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

最佳答案

尝试使用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.WindowsMediaPlayerC:\Windows\System32\wmp.dll对我不起作用,这就是为什么我使用 new:绰号的原因。

关于excel - 如何通过VBA异步循环在Excel中播放多个WAV文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443624/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com