Library tutorials & articles

Build an MP3 Player

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 

Comments

  1. 25 Aug 2009 at 02:30

    What version of VB are you running in order to rpoduce this player? I am running VB6 at the moment and cannot compile this player, any chance of rewriting the script for Vb6 for me please as i am only new to Vb and still trying to learn.

    Thanks, Jameson

Leave a comment

Sign in or Join us (it's free).

ElementK Journals
AddThis

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!