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