mediaplayer time in label

theatheist77

Member
Joined
Dec 1, 2011
Messages
10
Programming Experience
Beginner
hi everybody!

so i am busy with a mediaplayer ( just for fun ) and everything pretty much works, but one thing doesn't.

the thing is ive got 2 labels, the first label needs to show how much ive already played, and the second label needs to show the duration of the media file.
example: a media file from 1 hour 32 minutes and 18 second and i have already player 43 minutes and 9 seconds from it, then the first label needs to show 00:43:09 and the second label 01:32:18.

if somebody can help me out with this that would be very nice.
 
Ghetto Solution

Are you doing this in regular VB or online? I imagine the postback would be a nightmare in an ASP.NET environment. Here's my ghetto solution. Sorry, but I only sank about ten minutes into this....

Add a timer. Set the interval to 1000 milliseconds...or one second....

Here are your subs....

VB.NET:
Public Class Form1
    Dim z As Integer = 10
    Dim y As Integer = 10
    Dim x As Integer = 10
    Dim w As Integer = 10

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Static j, k, l, m As Integer
        j += 1
        Label1.Text = "0:0" & j.ToString
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'wx:yz
        z -= 1
        If z = 0 Then
            z = 10
            y -= 1
            If y = 0 Then
                y = 10
                x -= 1
            End If
            'If x = 0 Then
            '   .......
        End If
        Label2.Text = w.ToString & x.ToString & ":" & y.ToString & z.ToString
    End Sub
End Class

That should get you started. There are FAR better ways of doing this, but like I said.... Ghetto solutions sometimes work. Especially if it's just for your own personal amusement.... ;-)

Think absolute values too. That's bumping around in my mind now......
 
Are you doing this in regular VB or online? I imagine the postback would be a nightmare in an ASP.NET environment. Here's my ghetto solution. Sorry, but I only sank about ten minutes into this....

Add a timer. Set the interval to 1000 milliseconds...or one second....

Here are your subs....
VB.NET:
Public Class Form1
    Dim z As Integer = 10
    Dim y As Integer = 10
    Dim x As Integer = 10
    Dim w As Integer = 10

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Static j, k, l, m As Integer
        j += 1
        Label1.Text = "0:0" & j.ToString
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'wx:yz
        z -= 1
        If z = 0 Then
            z = 10
            y -= 1
            If y = 0 Then
                y = 10
                x -= 1
            End If
            'If x = 0 Then
            '   .......
        End If
        Label2.Text = w.ToString & x.ToString & ":" & y.ToString & z.ToString
    End Sub
End Class

That should get you started. There are FAR better ways of doing this, but like I said.... Ghetto solutions sometimes work. Especially if it's just for your own personal amusement.... ;-)

Think absolute values too. That's bumping around in my mind now......

thx man it works!
 
now i only got one problem, when i move the trackbar how can the time go with it?
btw sorry for my bad english.

E gads man! I hope you made some modifications to that code! What I sent you was just a mock up!!! ;-) I was thinking about this a little more and you may want to make an object out the timer. That way you can do things like MyTimer.Start(), MyTimer.Pause(), etc. and make the functionality a little easier to manage. If I get around to it, I'll take a crack at it. As per making the timer move when you move the scroll bar, I'm not sure what kind of control you're using for that. Never played around with sound much beyond error beeps. Are there properties like ScrollBar.Length and ScrollBar.Value? If that's the case, you could just write a mathematical formula that adjusts the counter based on the percentage value of where the scroll bar is....

This is hypothetical, but I imagine it would look something like this.....

VB.NET:
Dim decScrollPercentage, decTotalSeconds As Decimal
Dim intCurrentPlace As Integer
decScollPercentage = ScrollBar.Value / ScrollBar.Length
decTotalSeconds = FindSeconds(strLengthOfSong) 'I'm guessing you specify this somewhere in your program.
intCurrentPlace = Cint(decTotalSeconds * decScrollPercentage)
lblTimer.text = RecalibrateTimer(intCurrentPlace)

Fuction FindSeconds (byVal SongLength As String) As Decimal
     Dim Minute, Seconds As Decimal
     'Something like....
     Minutes = SongLenth.Substring(0, 2)
     Seconds = SongLenth.Substring(4, 2) 'Obvious you skip the third place as that's your ":"
     Return (Minutes * 60) + Seconds
End Function

Function RecalibrateTimer(byVal TotalSeconds As Integer) As String 
     Dim Minutes, Seconds As Integer
     Dim NewTimer As String
     Minutes = TotalSeconds / 60
     Seconds = TotalSeconds Mod 60
     NewTimer = Minutes.toString & ":" & Seconds.toString
     Return NewTimer
End Function

Again, this all is hypothetical. Kinda depends if you can get the total length and current value of your scroll bar.... I also worry about the precision of this method. You could wind up off by a second or so.....

P.S. Your English is better than a fair amount of the native speakers I work with..... ;-)
 
Back
Top