Ron
Ron

Reputation: 4095

Update mediaElement based on slider value

I have a mediaElement and a slider.
The slider's maximum value is video's duration in seconds (for example if the video is 2 minutes, the slider's value is 120).

I want to update the mediaElement.Position based on the slider's value but the problems is that I dont want to update the position until the user FINISHED manipulating the value.

so what I did is 2 functions:

private void DurationSlider_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    SeekToMediaPosition();
}

this function applys if the user stopped manipulating the slider.

private void DurationSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
     SeekToMediaPosition();
}

this function applys if the user clicked on different value in the slider.

The problem is that they clash.
Manipulating the slider cause the value to be changed...

so What I did is added this function:

bool manipulating = false;
private void DurationSlider_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
    manipulating = true;
}

and in the ValueChanged function I checked whether manipulating = true or not
It solved half of the problem - it does not change the video position while manipulating (until I finish) but if I click on different position on the slider (without releasing the mouse) and continue manipulating the slider's value - it changes the video position to where I clicked on the slider and then changes again to where I ended the manipulation.

So whats wrong?
I want to change the video position ONLY when the user released the mouse. I cant find event that fires what I want...

Upvotes: 1

Views: 1519

Answers (1)

Naren
Naren

Reputation: 2311

You could use PointerCaptureLost Event to determine the position after the user finished dragging the control.

<Slider PointerCaptureLost=="Slider_PointerCaptureLost"  
        Height="27" Margin="132,162,0,0" VerticalAlignment="Top" Width="303"/>

Then in code behind.

    private void Slider_PointerCaptureLost(object sender, DragCompletedEventArgs e)
    {
        Slider s = sender as Slider;
        // Your code
        MessageBox.Show(s.Value.ToString());
    }

Upvotes: 1

Related Questions