Search code examples
c#wpfevent-handlingmediaelementreplay

Replaying a video continuously in a WPF media element


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?


Solution

  • 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();