Playing Time
Determining the song's duration
Now that the code has opened the file, we can determine its duration. Whenever
a file's open state changes, Media Player triggers an OpenStateChange() event.
We'll use this event to calculate the song's duration. Listing D shows this
event. As you can see, this event also sets the FileOpen variable.
Listing D: Calculating the duration
Private Sub MediaPlayer1_OpenStateChange(ByVal _
OldState As Long, ByVal NewState As Long)
Min = MediaPlayer1.Duration * 60
Sec = MediaPlayer1.Duration - (Min * 60)
lblTotalTime = "Total Time: " & Format(Min, "0#") _
& ":" & Format(Sec, "0#") 'format time to 00:00
FileOpen = CBool(NewState)
End Sub
Keeping track of play time
As one of our final touchups to the application, we can provide the current
running time. Earlier, we placed two labels and a timer control on the form
for just such a purpose. Listing E contains the code for this feature.
Listing E: Displaying the elapsed time
Private Sub Timer1_Timer()
Min = MediaPlayer1.CurrentPosition * 60
Sec = MediaPlayer1.CurrentPosition - (Min * 60)
If Min > 0 Or Sec > 0 Then
lblElapsedTime = "Elapsed Time: " & Format(Min, "0#") _
& ":" & Format(Sec, "0#")
Else
lblElapsedTime = "Elapsed Time: 00:00"
End If
End Sub