Replaying a video continuously in a WPF media element

Anoushka Seechurn picture Anoushka Seechurn · Nov 15, 2013 · Viewed 11.4k times · Source

I have a video file playing in a media element. I need to keep it playing, thus I tried:

me.play();
me.MediaEnded += new RoutedEventHandler(me_MediaEnded);

With this event method:

//loop to keep video playing continuously
void me_MediaEnded(object sender, EventArgs e)
{
    //play video again
    me.Play();
}

However the above does not replay the video file. Why? What did I do wrong?

Answer

Grant Winney picture Grant Winney · Nov 15, 2013

According to a post on MSDN:

Play() starts from the current position therefore you have to first go to the starting place and then play it again.

So you have to reset the position before replaying:

me.Position = TimeSpan.FromSeconds(0);
me.Play();