使用循环播放MP3文件

时间:2020-09-02 08:01:07

标签: excel vba mp3

我正在研究一种代码,该代码将根据A列中这些mp3文件的文件名列表来播放特定目录中的MP3文件。该代码对大约五个或六个文件有效,然后excel挂起了一段时间然后从另一个意外点恢复。我该如何修复代码,以使其能够播放A列中的所有mp3文件?

Public wmp As Object

Sub Test()
    Dim r As Long
    Set wmp = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        With wmp
            .URL = ThisWorkbook.Path & "\Media\" & Cells(r, 1).Value & ".mp3"
            .Controls.Play
        End With
        DoEvents
        Application.Wait Now + TimeValue("00:00:02")
    Next r
End Sub

1 个答案:

答案 0 :(得分:2)

您可以使用循环来建立播放列表,然后播放

Public wmp As Object

Sub Test()
    Dim r As Long
    Dim itm
    Set wmp = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        With wmp
            Set itm = .newMedia(ThisWorkbook.Path & "\Media\" & Cells(r, 1).Value & ".mp3")
            .currentPlaylist.appendItem itm
        End With
    Next r
    wmp.Controls.Play
End Sub