Question Need Help With Track Bar In Media Player

osher

New member
Joined
Mar 15, 2010
Messages
3
Programming Experience
1-3
So basicly ive made a media player in vb using the windows media player control and everything is fine except that when i use the trackbar to navigate on a video it starts stopping and starting like a scratched cd (there is nothing wrong with the video file) strangely this does not happen whilst playing audio files the shaking stops if i pause it for a second ant then play it again but incorporating a pause in the trackbar function does not help. This the code i found on the internet but this produces the same result:
Try
If (PlayerControl.currentMedia.duration <> 0) Then
Dim NewPerc As Double = Convert.ToDouble(PlayBar.Value) / 100
Dim DurationVar As Integer = Convert.ToInt3 _(PlayerControl.currentMedia.duration * 1000) 'milliseconds

Dim NewPos As Integer = (DurationVar * NewPerc) / 1000

PlayerControl.Ctlcontrols.currentPosition = NewPos
Else
PlayBar.Value = 0
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

Im really stumped :( can som1 help ?
 

wearmc

Member
Joined
Mar 28, 2010
Messages
6
Programming Experience
1-3
RE: Trackbar in Media Player

Hi Osher

Below is the method I used to get a trackbar working on my form. Allows you to advance and rewind through video as well as auto updating while video is playing. I'm using Visual Basic 2008 Express Edition.

Add a timer control to your form with the interval set at 100. This is required for updating the trackbar.

in your players playstatechanged code add
VB.NET:
If Me.player.playState = WMPLib.WMPPlayState.wmppsBuffering Or Me.player.playState = WMPLib.WMPPlayState.wmppsPlaying Then
' set trackbar1 min and maximum values
Me.TrackBar1.Minimum = 0
Me.TrackBar1.Maximum = Me.player.currentMedia.duration
me.timer1.start()
ElseIf Me.player.playState = WMPLib.WMPPlayState.wmppsMediaEnded Or Me.player.playState = WMPLib.WMPPlayState.wmppsStopped Then
Me.TrackBar1.Value = 0
me.timer1.stop()
end if

in the timer_tick code for your newly added timer enter
VB.NET:
'adjust trackbar1 value for current media position
If player.playState = WMPLib.WMPPlayState.wmppsPlaying Then
Me.TrackBar1.Value = Me.player.Ctlcontrols.currentPosition
end if

finally to advance/rewind your video using the trackbar use the following
VB.NET:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Me.player.Ctlcontrols.currentPosition = Me.TrackBar1.Value
End Sub

Hope this helps. Any questions feel free to ask.

Regards
Colin
 

KasperC

Member
Joined
Mar 28, 2010
Messages
8
Programming Experience
3-5
The problem may keep occuring. It is actually a problem in the Windows Media Player Control. While you hold your mouse down navigating with your trackbar it constantly sends commands to the player to change position. Because a video takes more time to render for the player when jumping to another point it will play the previous point where it managed to render until it has rendered at the new point. Audio renders much faster so it can jump around as fast as it is getting commands.

If you want to eliminate this. When the above code doesn't. You could add a pause command to the MouseDown event of your trackbar and a play command to the MouseUp event.
 

osher

New member
Joined
Mar 15, 2010
Messages
3
Programming Experience
1-3
Thanks Kasper ill try that it sounds like it'll work but won't it be a bit inconvenient for the user if it keeps stopping every time he changes the trackbar ?
 

KasperC

Member
Joined
Mar 28, 2010
Messages
8
Programming Experience
3-5
You could also wait for the MouseUp event before sending the command to change progress. This is how WMP itself works.
 

osher

New member
Joined
Mar 15, 2010
Messages
3
Programming Experience
1-3
Thanks KasperC that was the last think i needed to finish my project :)

p.s soz i took a while to reply hav been quite busy
 
Top Bottom