get details of song being played (1 Viewer)

smiler44

Registered User.
Local time
Today, 17:29
Joined
Jul 15, 2008
Messages
641
This is the situation.
I have selected some songs to play via windows media player, they start playing.


I want the songs to play for x number of minutes. so I start excel tell it how long to play for and run the macro.


I cannot work out how to :-
1. get the duration of the song being played
2. get the title of the song being played

if you could help with either I'd be grateful. 1 is my first choice to be solved
but from 2 could possibly solve 1



I could solve this with a timer counting down to zero but would prefer to solve it but solving 1.

thank you

smiler44
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
Have a look here re the links offered by Daniel Pineault.
 

June7

AWF VIP
Local time
Today, 09:29
Joined
Mar 9, 2014
Messages
5,423
Only place I can find duration is Windows file properties. Example:

Code:
Sub GetLength()
With CreateObject("shell.application").Namespace("C:\Users\June\")
    Debug.Print Mid(Join(Filter(Split(.GetDetailsOf(.Items.Item("Test.mp3"), -1), vbLf), "Length: "), ""), 9)
End With
End Sub
How are you selecting files to play? Are you using ActiveX Windows Media Player control embedded in worksheet? I have tested WMP control embedded in Access form.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
June7,
I've just tried it on one of my mp3 files that does have the duration in it and I get nothing.? :confused:
Code:
Sub GetLength()
Dim oFolder As Object
'Set oFolder = CreateObject("shell.application").Namespace("M:\ABBA\Gold Greatest Hits\")
With CreateObject("shell.application").Namespace("M:\ABBA\Gold Greatest Hits\")
    Debug.Print Mid(Join(Filter(Split(.GetDetailsOf(.Items.Item("01 - Dancing Queen.mp3"), -1), vbLf), "Length: "), ""), 9)
End With
End Sub
 

June7

AWF VIP
Local time
Today, 09:29
Joined
Mar 9, 2014
Messages
5,423
I tested with an mp4 and it did work so I assumed mp3 would. So just tested with mp3 and it also works for me.
 

isladogs

MVP / VIP
Local time
Today, 17:29
Joined
Jan 14, 2017
Messages
18,186
All details about artist, title, duration, bit rate etc for audio files are all available as extended properties of the file and can be obtained using the GetDetailsOf method.
Some time ago I created an example database containing all you need to do this. It can be used to get the extended properties for any type of file.
You can find this app both here https://www.access-programmers.co.uk/forums/showthread.php?t=294548 and on my website http://www.mendipdatasystems.co.uk/extended-file-properties/4594398115

Hope that is of use to you
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
Hmm, what am I doing wrong:banghead:

I did discover that it is in item 27 though?
Code:
Sub GetLength()
Dim oFolder As Object
Dim varItem As Variant
Dim i As Integer

'Set oFolder = CreateObject("shell.application").Namespace("D:\Music\ABBA\Gold Greatest Hits\")
With CreateObject("shell.application").Namespace("M:\ABBA\Gold Greatest Hits\")
    For i = -1 To 30
        Debug.Print i & " " & .GetDetailsOf(.Items.Item("01 - Dancing Queen.mp3"), i) ', vbLf), "Length: "), "")
    'Debug.Print Mid(Join(Filter(Split(.GetDetailsOf(.Items.Item("01 - Dancing Queen.mp3"), -1), vbLf), "Length: "), ""), 9)
    Next
End With
End Sub
-1 Type: MP3 Audio File (VLC)
Size: 3.55 MB
Date modified: ?18/?08/?2009 ??14:27
0 01 - Dancing Queen.mp3
1 3.55 MB
2 MP3 Audio File (VLC)
3 18/08/2009 14:27
4 18/08/2009 14:27
5 22/05/2019 22:53
6 N
7 Online
8 Online only
9 Audio
10 Unix User\nobody
11 Music
12
13 ABBA
14 Gold: Greatest Hits
15 1993
16 Rock/Pop
17
18
19 Unrated
20 ABBA
21 Dancing Queen
22
23
24
25
26 1
27 00:03:52
28 ?128kbps
29 No
30
 

isladogs

MVP / VIP
Local time
Today, 17:29
Joined
Jan 14, 2017
Messages
18,186
There are a total of 310 or so extended properties so stopping at number 30 means you may be missing several properties related to MP3 files
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
I tested with an mp4 and it did work so I assumed mp3 would. So just tested with mp3 and it also works for me.

June7
Would I need a particular reference? I am puzzled as I just copied your code and changed the filename, so keen to find out why it does not work.
 

June7

AWF VIP
Local time
Today, 09:29
Joined
Mar 9, 2014
Messages
5,423
I did not add any library. Just tested in a brand new db and works.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
There are a total of 310 or so extended properties so stopping at number 30 means you may be missing several properties related to MP3 files

Thanks Colin,
That was just to see where my code was not working. I started at 20 and did not see the duration and so upped it to 30, where I did.
Good to know there the limit though.
 

smiler44

Registered User.
Local time
Today, 17:29
Joined
Jul 15, 2008
Messages
641
Only place I can find duration is Windows file properties. Example:

Code:
Sub GetLength()
With CreateObject("shell.application").Namespace("C:\Users\June\")
    Debug.Print Mid(Join(Filter(Split(.GetDetailsOf(.Items.Item("Test.mp3"), -1), vbLf), "Length: "), ""), 9)
End With
End Sub
How are you selecting files to play? Are you using ActiveX Windows Media Player control embedded in worksheet? I have tested WMP control embedded in Access form.

i am selecting songs to play using windows explorer, opening the directory, selecting the songs and pressing play.

am i right in thinking your code requires me to know the title of the song?
This is something i will need to obtain via code, i cannot pre program it

i am not using Access just using excel, i'm no good with Access

smiler44
 

smiler44

Registered User.
Local time
Today, 17:29
Joined
Jul 15, 2008
Messages
641
i created a marco with help from here and the internet that randomly plays songs for a pre determined time.

i hope this is all of it. it may look as if i declare part way through a module but in fact this code is over 2 or more modules and the declarations are at the top

Code:
Dim fd 'file duration of song

Dim filetoplay ' the mp3 file to play
Global spt As String


Sub start()
' play music for pre determined time

Sheet1.Range("A1").Value = "00:00:00"
Sheet1.Range("A2").Value = "00:00:00"
Sheet1.TextBox2.Value = "00:00:00"

Call randomlyselectsong

If Sheet1.CheckBox1.Value = True Then ' shutdown laptop
Call shutdown 'shut down laptop
End If
'MsgBox ("finished")
End Sub





Sub durationplayed()



'how many minutes music has played for after selected song plays
Dim fdsec As Integer ' file durtion seconds 00:10
Dim fdm As Integer 'file duration minutes 10:00

Dim sec As Integer '  seconds 00:10
Dim m As Integer 'minutes 10:00
Dim mm

'fd can be found in fileduration sub
'fd = "3:33"

fdsec = Mid(fd, 3, 2) 'gets file duration seconds
fdm = Left(fd, 1) 'gets fie duration minutes

sec = Mid(Sheet1.TextBox2, 4, 2) 'gets seconds in text box 2
m = Left(Sheet1.TextBox2, 2) 'gets mintes in text box 2

mm = fdm + m & ":" & fdsec + sec

If fdsec + sec > 60 Then ' if seconds are more then 60
m = m + 1
mm = fdm + m & ":" & fdsec + sec - 60
End If

Sheet1.TextBox2.Value = mm

End Sub

Sub shutdown()

Shell ("taskkill /f /im wmplayer.exe") 'shutdown windows media player
Shell "shutdown -s -t 05", vbHide 'shut down laptop
Application.Quit
End Sub


Dim fs, f1, fc, s
Dim ftp
Dim b 'amount of time to pause for while song plays
'other wise next song play immediately


'used with randum play for set time module



Public Sub randomlyselectsong()

Dim mm ' minutes
Dim ss ' seconds

Do Until Sheet1.TextBox2.Value > Sheet1.TextBox1.Value

Set fs = CreateObject("scripting.filesystemobject")

Set f = fs.GetFolder("C:\path\directory where songs are kept")

Randomize
i = CInt((Rnd() * f.Files.Count) + 1)
j = 1
For Each fi In f.Files
If j = i Then

ftp = fi.Name 'fi, randomly selected song

Call RecursiveDir(Directory)

filetoplay = """C:\path\directory where songs are kept Music\" & ftp
Shell "C:\path\Documents\wmplayer /play /close " & filetoplay

''''''''''''''''''''''''''''''''''''''''
   'Sheet1.TextBox2.Value = Range("a3").Text
   Range("a2").Value = Sheet1.TextBox2.Value 'time music played before current song
   Sheet1.TextBox2.Value = Range("a3").Text ' total time music played after current song
ss = Right(b, 2) ' gets far right 2 charectors

mm = Mid(b, 4, 2) 'starting at 4 from left, gets the 4th and 5th charector

b = mm * 60 + ss
          
pause (b) ' how long to wait in seconds before playing next song
'''''''''''''''''''''''''''''''''''''''''
End If
j = j + 1
Next

Loop
End Sub

Public Sub RecursiveDir(ByVal currdir As String)

  Range("A1").Select
  Range("a1").Value = FileInfo("C:\path\directory where songs are kept\", ftp, 27)
             
End Sub

Function FileInfo(path, filename, item) As Variant
' this gets the file duration
    Dim objShell As IShellDispatch4
    Dim objFolder As Folder3
    Dim objFolderItem As FolderItem2
      
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(path)
    Set objFolderItem = objFolder.ParseName(filename)
    
    FileInfo = objFolder.GetDetailsOf(objFolderItem, 27)
    b = FileInfo ' song run time. used to pause macro while song plays
           
    Set objShell = Nothing
    Set objFolder = Nothing
    Set objFolderItem = Nothing
End Function


Sub pause(seconds As Single)
  Dim TimeEnd As Long
           
           TimeEnd = Timer + seconds
        
            If TimeEnd > 86390 Then
                TimeEnd = 0
            End If
                
            Do
                DoEvents
            Loop Until TimeEnd <= Timer
        
        End Sub

smiler44
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
Probably better to upload the file?
 

isladogs

MVP / VIP
Local time
Today, 17:29
Joined
Jan 14, 2017
Messages
18,186
I realise you want this for Excel.
However if you run my app in Access, select the folder(s) continuing your songs, you can get all the track details for the entire set of files at once with no code needed. Then export the data to Excel. Job done!
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:29
Joined
Sep 21, 2011
Messages
14,044
What is this meant to be doing?
Code:
''''''''''''''''''''''''''''''''''''''''
   'Sheet1.TextBox2.Value = Range("a3").Text
   Range("a2").Value = Sheet1.TextBox2.Value 'time music played before current song
   Sheet1.TextBox2.Value = Range("a3").Text ' total time music played after current song
ss = Right(b, 2) ' gets far right 2 charectors

mm = Mid(b, 4, 2) 'starting at 4 from left, gets the 4th and 5th charector

b = mm * 60 + ss
          
pause (b) ' how long to wait in seconds before playing next song
 

smiler44

Registered User.
Local time
Today, 17:29
Joined
Jul 15, 2008
Messages
641
I realise you want this for Excel.
However if you run my app in Access, select the folder(s) continuing your songs, you can get all the track details for the entire set of files at once with no code needed. Then export the data to Excel. Job done!

isladogs, may be i dont quite understand. Once I have the attributes what can I do with them? I dont know (my code does not know) what song is being played and so cannot look the attributes up for that song.


I need to obtain via code the duration of the song currently being played or the title of the song being played so I can look the duration up.

perhaps instead of selecting the songs via windows explorer i should do it via Excel where i could already have obtained the duration...... thats a way to do it but i wont learn much that way :)

If i go with my original method i would learn more I think..... but then???

smiler44
 

smiler44

Registered User.
Local time
Today, 17:29
Joined
Jul 15, 2008
Messages
641
What is this meant to be doing?
Code:
''''''''''''''''''''''''''''''''''''''''
   'Sheet1.TextBox2.Value = Range("a3").Text
   Range("a2").Value = Sheet1.TextBox2.Value 'time music played before current song
   Sheet1.TextBox2.Value = Range("a3").Text ' total time music played after current song
ss = Right(b, 2) ' gets far right 2 charectors

mm = Mid(b, 4, 2) 'starting at 4 from left, gets the 4th and 5th charector

b = mm * 60 + ss
          
pause (b) ' how long to wait in seconds before playing next song


i think it pauses excel for the duration of the current song being played so it finishes before excel plays the next song.
i can also see how long music has been playing for before and after the current song

smiler44
 

Users who are viewing this thread

Top Bottom