Play MP3 files using loops

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Play MP3 files using loops

Post by YasserKhalil »

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?

Code: Select all

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
https://stackoverflow.com/questions/63701170/
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 ..

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15615
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Play MP3 files using loops

Post by ChrisGreaves »

YasserKhalil wrote:
05 Sep 2020, 18:10
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?
Hi Yasser.
I have done much what you wanted to do via Word/VBA accessing an MSAccess data base as the source of file names.

I grab a sample of tracks whose date-last-played is quite old then play them, one by one, using the OnTime command. (attached).

Mind you, OnTime in MSWord is trickier than in Excel, because in MSWord one can have only one OnTime event at a time.
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Play MP3 files using loops

Post by HansV »

I think the problem is that the code continues immediately after calling .Controls.Play, so if the .mp3 file is longer than 2 seconds, the code sends the next .Controls.Play command before the current one has finished.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Play MP3 files using loops

Post by YasserKhalil »

Thanks a lot. I have no great experience at access.
I think the problem is that the code continues immediately after calling .Controls.Play, so if the .mp3 file is longer than 2 seconds, the code sends the next .Controls.Play command before the current one has finished.
Is there any workaround to add waiting time between each file?

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Play MP3 files using loops

Post by HansV »

There is undoubtedly another Windows API function for that, but I cannot help you with that.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15615
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Play MP3 files using loops

Post by ChrisGreaves »

YasserKhalil wrote:
05 Sep 2020, 20:28
Thanks a lot. I have no great experience at access. Is there any workaround to add waiting time between each file?

Code: Select all

Application.OnTime when:=Now + TimeSerial(0, 0, lngcDelayBetweenTracks) + dtDuration, Name:="PlayDateLastPlayed", tolerance:=60
Cheers
Chris
There's nothing heavier than an empty water bottle

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Play MP3 files using loops

Post by YasserKhalil »

Thanks Chris.
Can you provide me with the whole code that works with excel ?

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Play MP3 files using loops

Post by YasserKhalil »

I found this

Code: Select all

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Dim vPlay, sMusicFile As String

Sub Play_MIDI_MP3_File_API_mciSendString_Library()
    Dim r As Long
    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    sMusicFile = ThisWorkbook.Path & "\Media\" & Cells(r, 1).Value & ".mp3"
    vPlay = mciSendString("Play " & sMusicFile, 0&, 0, 0)
    If vPlay <> 0 Then Debug.Print "Can't Play!"
    Next r
End Sub
But couldn't control the code. I need to wait the file to be finished then wait a while then play another new file.

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Play MP3 files using loops

Post by YasserKhalil »

This resolved the problem

Code: Select all

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Dim vPlay, sMusicFile As String

Sub Test()
    Dim sFile As String, r As Long
    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        sFile = Replace(Cells(r, 1).Value, "?", "")
        sMusicFile = ThisWorkbook.Path & "\Media\" & sFile & ".mp3"
        vPlay = mciSendString("Play " & sMusicFile & " wait", 0&, 0, 0)
        If vPlay <> 0 Then Debug.Print "Can't Play " & sFile
        Application.Wait Now + TimeValue("00:00:02")
    Next r
End Sub

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15615
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Play MP3 files using loops

Post by ChrisGreaves »

YasserKhalil wrote:
06 Sep 2020, 03:20
Thanks Chris.Can you provide me with the whole code that works with excel ?
Yes, Yasser. It is in that attached text file.
I believe that you have your Excel project running in the sense that you can obtain a file name from Excel, and that you can play that file within/from Excel.
My procedure obtains the file name from Access and plays the file from within Word.
Please read the text file and drill in to that "On Time" statement where I use a file name (which you already have from Excel) and try playing the file using Application.OnTime (as I have shown) to make WinAmp or a similar player play the file.
You need to extract the <duration> (or playing-time) of the file, and set a constant "lngcDelayBetweenTracks" to a number of around five (seconds), and it should work.

You are, in a way, better off than I, because Excel supports (up to ten, I think) multiple OnTime events. Word supports only one OnTime event and I use OnTime in other concurrent Word applications, I had to write a mini-system, standalone, called "Timer" which could police with multiple OnTime events from separate Word2003 applications.
Cheers
Chris
There's nothing heavier than an empty water bottle