Slider
Last post 05-16-2008 2:59 AM by mchlsync. 11 replies.
Sort Posts:
03-27-2008 2:14 AM
Slider

 i created one media player using silverlight 2.0 i have problem with slider for progress of video

1. i have code for auto progress with progress of video

2. i have code for change the video position when we grab the slider and change the position for forward and backward 

but i am not able apply together please tell me c# code for this

i used for auto progress Storyboard with complete event

and change position for video i used slider and event MouseLeftButtonDown  

AshokSindhi

Joined on 03-17-2008
Posts 18
03-31-2008 7:40 AM
Marked as Answer
Re: Slider

Hello, MediaElement doesn't expose an event when its Position changes. You'll have to use a DispatcherTimer to query its Position from time to time. To begin, you can handle the MediaElement's MediaOpened event to get the video's length. Suppose your MediaElement is named MainMediaElement, and your Slider is named SliderProgress:

         private void MainMediaElement_MediaOpened(object sender, RoutedEventArgs e)

         {

              SliderProgress.Maximum = MainMediaElement.NaturalDuration.TimeSpan.TotalSeconds;

              SliderProgress.Value = 0;

         } 

Then you can create a DispatcherTimer. 

        DispatcherTimer timer = new DispatcherTimer();      

        timer.Interval = TimeSpan.FromMilliseconds(100);

        timer.Tick += new EventHandler(Timer_Tick);  

Then handle the MediaElement's CurrentStateChanged event to start/stop the timer. 

         private void MainMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)

         {

              if (MainMediaElement.CurrentState == MediaElementState.Playing)

              {

                   timer.Start();

              }

              else

              {

                   timer.Stop();

              }

          } 

In the timer's Tick event, set the Slider's Value. 

         private void Timer_Tick(object sender, EventArgs e)

         {

              SliderProgress.Value = MainMediaElement.Position.TotalSeconds;

         }

Now the Slider's Value will change according to the MediaElement's Position. To control the MediaElement's Position with the Slider?, you must handle its MouseLeftButtonDown event to stop the timer, and handle MouseLeftButtonUp event to restart the timer. 

         private bool changedByUser; 

         private void SliderProgress_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

         {

              changedByUser = true;

              timer.Stop();

         }

 

         private void SliderProgress_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

         {

              if (changedByUser && MainMediaElement.CurrentState == MediaElementState.Playing)

              {

                   MainMediaElement.Position = TimeSpan.FromSeconds(SliderProgress.Value);

                   timer.Start();

                   changedByUser = false;

              }

         }

Hope it helps.

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Joined on 10-29-2007
Posts 1,084
04-30-2008 2:25 AM
Re: Re: Slider

great solution.Was simply using rectangles inplace of the inbuilt slider feature.IT works!

anukit

Joined on 04-17-2008
Posts 1
05-15-2008 5:07 AM
Re: Re: Re: Slider

how would i do this in visual basic?

 i'm having trouble converting the code :(

Xeron

Joined on 05-13-2008
Posts 16
05-15-2008 5:22 AM
Re: Re: Re: Slider

Xeron:

how would i do this in visual basic?

 i'm having trouble converting the code :(

 

Can you try to use C# to VB.NET converter? If you are still getting the error, please show us your converted code and the error messages. 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Joined on 09-16-2005
Singapore
Posts 2,021
05-15-2008 5:29 AM
Re: Re: Re: Slider

Well really I'm having trouble with one line

C#
timer.Tick += new EventHandler(Timer_Tick);

VB
AddHandler timer.tick, (what goes here???)  

Xeron

Joined on 05-13-2008
Posts 16
05-15-2008 7:13 AM
Re: Re: Re: Slider

Xeron:
AddHandler timer.tick, (what goes here???)  
 

Please try this code. 

AddHandler timer.Tick, AddressOf Timer_Tick

 

Ref:: http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Joined on 09-16-2005
Singapore
Posts 2,021
05-15-2008 7:39 AM
Re: Re: Re: Slider

i tried & it doesnt work. here is my full code:

Private Sub VideoBlock_MediaOpened(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     ProgressSlide.Maximum = VideoBlock.NaturalDuration.TimeSpan.TotalSeconds

     ProgressSlide.Value = 0

End Sub

Dim progressTimer As New System.Windows.Threading.DispatcherTimer()

Dim handler As EventHandler

Public Sub dTimer(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     progressTimer.Interval = TimeSpan.FromMilliseconds(100)

     AddHandler progressTimer.Tick, handler

End Sub

Private Sub dTimer_Tick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     ProgressSlide.Value = VideoBlock.Position.TotalSeconds

End Sub

Private Sub VideoBlock_MediaEnded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     VideoBlock.Stop()

End Sub

Private Sub VideoBlock_CurrentStateChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     If (VideoBlock.CurrentState = MediaElementState.Playing) Then

          progressTimer.Start()

     Else

          progressTimer.Stop()

     End If

End Sub

Dim changedByUser As Boolean

Private Sub ProgressSlide_MouseLeftButtonDown (ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

     changedByUser = True

     progressTimer.Stop()

End Sub

Private Sub ProgressSlide_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

     If (changedByUser And VideoBlock.CurrentState = MediaElementState.Playing) Then

          VideoBlock.Position = TimeSpan.FromSeconds(ProgressSlide.Value)

          progressTimer.Start()

          changedByUser = False

     End If

End Sub

Xeron

Joined on 05-13-2008
Posts 16
05-15-2008 8:27 AM
Re: Re: Re: Slider

mchlsync:

Xeron:
AddHandler timer.tick, (what goes here???)  
 

Please try this code. 

AddHandler timer.Tick, AddressOf Timer_Tick

 

Ref:: http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx 

 that actually allows me to compile. but for some reason my slider doesnt work & my app crashes when i click the play button. i'd prob be the best off if someone could convert that entire piece of C# code

Xeron

Joined on 05-13-2008
Posts 16
05-15-2008 8:28 AM
Re: Re: Re: Slider

seems like you didn't notice "AddressOf"  Sad

 Please try this code. HTH

  AddHandler progressTimer.Tick, AddressOf dTimer_Tick

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Joined on 09-16-2005
Singapore
Posts 2,021
05-15-2008 8:38 AM
Re: Re: Re: Re: Slider

yeah now i can compile & that is prob right, but now my app crashes when i play my .wmv file. whether it be autoplay or if i start it.

 EDIT: ok if I either do progressTimer.Start() or progressTimer.Stop(), it crashes my app with this error message:

Silverlight error message
ErrorCode: 4002
ErrorType: ManagedRuntimeError
Message: System.InvalidCastException: Unable to cast object type 'System.EventArgs' to tye 'System.Windows.RoutedEventArgs'.
at Zyon_Games.Page._Lambda$_1(Object a0, EventArgs a1)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int21 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Xeron

Joined on 05-13-2008
Posts 16
05-16-2008 2:59 AM
Re: Re: Re: Slider

Xeron:
Private Sub dTimer_Tick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

     ProgressSlide.Value = VideoBlock.Position.TotalSeconds

End Sub

 

I think this code is wrong. According to Yi-lun's sample, you should use EventArgs . not RoutedEventArgs.  


(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Joined on 09-16-2005
Singapore
Posts 2,021