get details of song being played (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
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.
Well I tried a new DB and it worked.
Tried it in my test DB and it worked, so even more puzzled. :banghead:
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
You are already doing that in sub FileInfo with variable b

Not exactly the best naming convention I admit. ;)

Also what reference is required for this ?

objShell As IShellDispatch4

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
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
What is your structure for your music.?
You call RecursiveDir yet it does not appear to walk through any folders.?
Do you just have a folder with all the music in it.?

My structure is
Music\Artist\Album\Tracks
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
I'm a little intrigued with this project. I remember you starting it a while back.

How do you manage file paths with spaces in them, as my WMP balks at those.?
I've fixed that problem, but was wondering how you coped.?

Also you pass in the name of the file you are going to open into FileInfo sub, so you know the name of the file and the same sub gives you the duration.? :confused:
 

isladogs

CID
Local time
Today, 02:03
Joined
Jan 14, 2017
Messages
13,611
Hi smiler
I assumed that you wanted to save the track info to Excel then use Excel as a media player somehow. That's certainly possible in Access. Doing that means the file info is extracted and displayed automatically.
However, as it appears that's not what you want, I'll drop out of this thread.
Good luck
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
Gents I must tell you I'm no expert in coding as you can see. I have never done it for a living but have used and crated macros for work. I've had no training and a lot of stuff even if I use it I dont understand it, mainly because I did not create it. I have found a lot of stuff on the internet and it has either done just what I need or I have managed to manipulate it do do what I want.

The code I posted above is a working project that does just what I want for that project but now i want another project and I'm lost in obtaining what should be very simple information.

smiler44
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
Well what you were originally asking for can be found in the FileInfo sub.

However you would need to understand what it does to be able to use it.
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
Well what you were originally asking for can be found in the FileInfo sub.

However you would need to understand what it does to be able to use it.
Am I right in thinking that the song title is known when the fileinfo function runs? In this new situation, I have not been able to acquire the title.
If you could help me get the title of the song being played by windows media player I'd appreciate it.

smiler44
 

June7

AWF VIP
Local time
Yesterday, 17:03
Joined
Mar 9, 2014
Messages
3,253
As suggested, why don't you provide the workbook for analysis?

The FileInfo function requires file name to be passed to it as argument. Yet I don't see the function called. The sub randomlyselectsong has the file name. It sets the variable filetoplay and sends that value to the player. This is where you should put code to get the file duration, just before Shell command opening the file in player. Modify the code I provided. It can be a function with arguments or include the 3 lines of code within your procedure. Only instead of Debug.Print, set a variable.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
June,
It is called from RecursiveDir sub.?

A lot of the variables are global.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
Am I right in thinking that the song title is known when the fileinfo function runs? In this new situation, I have not been able to acquire the title.
If you could help me get the title of the song being played by windows media player I'd appreciate it.

smiler44
You have it the wrong way round.
You tell WMP what to play, so at that point YOU know what the filename is.
 

June7

AWF VIP
Local time
Yesterday, 17:03
Joined
Mar 9, 2014
Messages
3,253
Okay, see that function now. Doesn't appear RecursiveDir is even using the value passed to it.
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
As suggested, why don't you provide the workbook for analysis?

The FileInfo function requires file name to be passed to it as argument. Yet I don't see the function called. The sub randomlyselectsong has the file name. It sets the variable filetoplay and sends that value to the player. This is where you should put code to get the file duration, just before Shell command opening the file in player. Modify the code I provided. It can be a function with arguments or include the 3 lines of code within your procedure. Only instead of Debug.Print, set a variable.
I am just tabbing through the code to make sure what i provided was working, have you tried the code?

Are we getting out wires crossed? Ignoring the code i have provided can you help with using vba code to get the name of the song currently being played by windows media player?

smiler44
 

June7

AWF VIP
Local time
Yesterday, 17:03
Joined
Mar 9, 2014
Messages
3,253
I am not interested in building workbook.

Already have the file name in sub randomlyselectsong. As I stated, put code there to pull duration.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
June7,
I've tweaked my copy a little to try and understand how it worked.

The call to RecursiveDir I changed to use f as that is the folder for the files in RandomlySelect sub?

That sub then uses that, plus ftp (the filename) and the item number.

It is very haphazard, and smiler44 has explained why.

To be fair to get it working as it is now would have been a monumental task for smiler44, so fair play. :D
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
I am just tabbing through the code to make sure what i provided was working, have you tried the code?

Are we getting out wires crossed? Ignoring the code i have provided can you help with using vba code to get the name of the song currently being played by windows media player?

smiler44
this is the code that my existing project uses

Code:
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

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:\users\Dirk pitt\Documents\Dads Music")

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:\users\Dirk pitt\Documents\Dads Music\" & ftp
Shell "C:\Users\Dirk Pitt\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:\Users\Dirk Pitt\Documents\Dads Music\", 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, 02:03
Joined
Sep 21, 2011
Messages
6,351
I am not interested in building workbook.

Already have the file name in sub randomlyselectsong. As I stated, put code there to pull duration.
No need June7, he has the duration already stored in b as well in randomlyselectsong. So in fact he has everything he was asking for, just did not realise it.
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
June7,
I've tweaked my copy a little to try and understand how it worked.

The call to RecursiveDir I changed to use f as that is the folder for the files in RandomlySelect sub?

That sub then uses that, plus ftp (the filename) and the item number.

It is very haphazard, and smiler44 has explained why.

To be fair to get it working as it is now would have been a monumental task for smiler44, so fair play. :D
Thank you Gasman, I was chuffed when it worked.

would i be taking the mickey if I asked what should it be :)

smiler44
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:03
Joined
Sep 21, 2011
Messages
6,351
What, what should be.?

I would walk through the code line by line with F8 and look and see what it does. Inspect the variables by either hovering over them or ? variablename in the Immediate window.

You can set breakpoints in the left column (red dot) and the code will stop there.

Do yourself a favour and get a book on Access. It will save you a load of time and effort.

Not being rude, so ignore the titles. There is even a pdf to start with.?

https://www.google.com/search?q=dummies+guide+to+access+2010&oq=dummies+guide+to+access&aqs=chrome.2.69i57j0l5.12501j0j7&sourceid=chrome&ie=UTF-8
 

smiler44

Registered User.
Local time
Today, 02:03
Joined
Jul 15, 2008
Messages
637
No need June7, he has the duration already stored in b as well in randomlyselectsong. So in fact he has everything he was asking for, just did not realise it.

Gasman, I think you may be misunderstanding or I am misunderstanding.
The code I have posted randomly selects a song from a directory and plays it.
If the duration of that song does not exceed the time to play songs for, it randomly plays another song and so it goes on.


In the macro i want, I have already manually opened the directory where the songs are stored, manually selected the songs I want and manually started playing them.

I now want to play songs for x time. The songs I have selected may take longer and so i need this new macro to now get the time of each song as it is played and when the total play time of each song played exceeds x time I want the macro to close windows media player. This I can do but I cannot start a macro to get the time of the song currently being played

ignoring any code i have posted, how do i get the duration of the song being played?


smiler44
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom