Play MP3 files using loops

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
I am working on a code that would play MP3 files from a specific directory based on a list of the file names of these mp3 files in column A. The code works fine for about five or six files then excel hangs for some time then resume from another unexpected point. How can I fix the code so as to make it play all the mp3 files in column A?
VBA Code:
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

I got an answer from this link
Play MP3 files using loops
but this stores all the files then play them all .. I need to set waiting time between each file and at the same time I need to select the cell that is playing ..
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I don't know why it's hanging. Try setting a reference to wpm.dll, using events along with OnTime.

Code in Worksheet class:
VBA Code:
Option Explicit

Private WithEvents wmp As WindowsMediaPlayer
Private r As Range

Sub StartPlaying()
    Set wmp = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    Set r = [a1]
    PauseAndPlay 0
End Sub

Private Sub wmp_PlayStateChange(ByVal NewState As Long)
    If NewState = 1 Then Application.OnTime Now, Name & ".PauseAndPlay"
End Sub

Public Sub PauseAndPlay(Optional PauseSecs As Integer = 2)
    If Len(r) = 0 Then Exit Sub
    Application.Wait Now + TimeSerial(0, 0, PauseSecs)
    wmp.Url = ThisWorkbook.Path & "\Media\" & r & ".m4a"
    wmp.Controls.Play
    Set r = r.Offset(1)
End Sub
 
Upvote 0
Thanks a lot. I am not accustomed to using classes. Can you give me an example of how to use the class in that way?
 
Upvote 0
The example does not refer to a user defined class, but a public worksheet class. The code goes in the worksheet's class module. Understood?
 
Upvote 0
Thanks a lot. I have found it References: Windows Media Player (wmp.dll)
And the code works well .. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top