Question

I was following a tutorial on how to control video using WPF mediaelement. For most parts, all is good except the rewind video function. I tried looking for solutions and came across on a thread that suggested pausing the video and changing the position of the video playback. So I tried this:

//timer updates the position of the slider
if(flagRewind == true)
{
    try
    {
        if (playVideoMediaElement.NaturalDuration.HasTimeSpan)
        {
            //step the video back every second
            playVideoMediaElement.Position -= TimeSpan.FromMilliseconds(1000);

            videoSlider.Value = playVideoMediaElement.Position.TotalSeconds;
            //we have re-winded to the zero position of the slider
            if (videoSlider.Value <= 0)
            {
                //the video is done playing
                playVideoMediaElement.Stop();
                //get tag of button click, to change button icon appropriately             (pause[||]/play [>])
                tag = playPauseVideoButton.Tag.ToString(); 
                //call method that changes the button image icon to either [||] or [>]
                ChangePlayPauseButtonImageIcon();
                IsPlaying(false);
                playPauseVideoButton.IsEnabled = true;
                //we are done re-winding
                flagRewind = false;   
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}  

This works but it's skipping frames, I tried using the speedration on a button click event like this:

private void slowPlayVideoButton_Click(object sender, RoutedEventArgs e)
{
  playVideoMediaElement.SpeedRatio = (double)-1;
}

but this just stops (freezes) the video. How can I use the speed ratio to rewind the video smoothly? #Thanks!

Was it helpful?

Solution

All I did was change the timer interval by increasing the TimeSpan value and I also changed the TimeSpan for the media element. Code now looks this:

if(flagRewind == true)
{
    //change the timer interval
    timer.Interval = TimeSpan.FromMilliseconds(50.0);
    try
    {
        if (playVideoMediaElement.NaturalDuration.HasTimeSpan)
        {
            //step the video back every second
            playVideoMediaElement.Position -= TimeSpan.FromMilliseconds(50.0);
            videoSlider.Value = playVideoMediaElement.Position.TotalSeconds;

            if (videoSlider.Value <= 0)
            {
                //the video is done playing
                playVideoMediaElement.Stop();

                tag = playPauseVideoButton.Tag.ToString(); 
                ChangePlayPauseButtonImageIcon();
                IsPlaying(false);
                playPauseVideoButton.IsEnabled = true;

                flagRewind = false;   
            }
        }

And this works perfectly fine for me and when the user presses the forward || play button I just default the timer interval and media element position values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top