- Local time
- Today, 02:03
- Sep 21, 2011
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???
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.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.
You have it the wrong way round.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.
I am just tabbing through the code to make sure what i provided was working, have you tried the code?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.
this is the code that my existing project usesI 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?
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
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.I am not interested in building workbook.
Already have the file name in sub randomlyselectsong. As I stated, put code there to pull duration.
Thank you Gasman, I was chuffed when it worked.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.
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.