Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play video backwards in WPF?

I want to smoothly play video in backwards direction in WPF. I am using MediaElement to play the video. I read this post which suggests changing MediaElement.Position periodically to mimic the rewinding behaviour.

I tried following code for changing position of MediaElement.Position

private void Button_Click(object sender, RoutedEventArgs e)
{
    mePlayer.Pause();                               //Pause the media player first
    double m = 1 / frameRate;                       //Calculate the time for each frame
    double t = 120;                                 //Total length of video in seconds
    mePlayer.Position = TimeSpan.FromMinutes(2);    //Start video from 2 min
    while (t >= 60)                                 //Check if time exceeds 1 min
    {
        t = t - m;                                  //Subtract the single frame time from total seconds
        mePlayer.Position = TimeSpan.FromSeconds(t);//set position of video
    }
}

In above code I am trying to play video backwards from 2 min to 1 min. It's giving me 'System.OverflowException' on mePlayer.Position = TimeSpan.FromSeconds(t).

If anyone know how play video backwards in WPF, please help me to achieve this effect. Thank you.

like image 855
Bhavesh Jadav Avatar asked Oct 31 '22 01:10

Bhavesh Jadav


1 Answers

To do it smoothly you should use a Timer. Assuming the frame rate is 24 fps, then it means that is one frame every 1/24 = 0.0416 second or approximately 42 millisecond. So if your timer ticks every 42ms you can move mePlayer.Position backward:

XAML:

<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4"
              LoadedBehavior="Manual" ScrubbingEnabled="True"/>

Code:

    System.Windows.Threading.DispatcherTimer dispatcherTimer;
    int t = 240000; // 4 minutes = 240,000 milliseconds

    public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        //tick every 42 millisecond = tick 24 times in one second
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42);

    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        // Go back 1 frame every 42 milliseconds (or 24 fps)
        t = t - 42;
        mePlayer.Position = TimeSpan.FromMilliseconds(t);
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        mePlayer.Play();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Pause and go to 4th minute of the video then start playing backward
        mePlayer.Pause();                               
        mePlayer.Position = TimeSpan.FromMinutes(4);
        dispatcherTimer.Start();
    }
like image 116
Bahman_Aries Avatar answered Nov 15 '22 03:11

Bahman_Aries