Replaying a video continuously in a WPF media element

11,600

Solution 1

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

Solution 2

You could also use a Storyboard to control and loop the video without needing to hook events or do any code-behind work. This is the recommended solution on MSDN and is a more elegant and proper solution for MVVM design.

Read more on MSDN here, includes examples:

http://msdn.microsoft.com/en-us/library/ms741866%28v=vs.100%29.aspx

Solution 3

I got it to work with this:

class MyMediaPlayer : MediaPlayer
{
    private bool looping;

    public MyMediaPlayer() : base()
    {
        looping = false;  
        base.MediaEnded += new EventHandler(mediaEnded);
    }

    public MyMediaPlayer(string _file) : base()
    {
        looping = false;
        base.Open(new Uri(_file, UriKind.Relative));
        base.MediaEnded += new EventHandler(mediaEnded);
    }   

    public bool Looping
    {
        get { return looping;}
        set { looping = value; }
    }

    public void playLooping()
    {
        looping = true;
        base.Play();
    }

    public void playLooping(string _file)
    {
        looping = true;
        base.Open(new Uri(_file, UriKind.Relative));
        base.Play();
    }

    public void play()
    {
        looping = false;
        base.Play();
    }

    public void play(string _file)
    {
        looping = false;
        base.Open(new Uri(_file, UriKind.Relative));
        base.Play();
    }

    public void stop()
    {
        looping = false;
        base.Stop();
    }

    private void mediaEnded(object sender, EventArgs e)
    {
        if(looping)
        {
            base.Position = new TimeSpan(0, 0, 0);
            base.Play();
        }
    }

}

Hope this answers your question.

Solution 4

I don't know the reason but this never worked for me with a GIF:

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

My GIF stops playing after the first iteration.

The solution is:

<MediaElement x:Name="me"
              MediaEnded="MediaElement_MediaEnded"
              LoadedBehavior="Play" />

Code:

private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
    me.Position = TimeSpan.FromMilliseconds(1);
}

1 ms is used while 0ms might stop the playback you cannot run it again with a 'Play' method.

Share:
11,600

Related videos on Youtube

Anoushka Seechurn
Author by

Anoushka Seechurn

Updated on June 04, 2022

Comments

  • Anoushka Seechurn
    Anoushka Seechurn almost 2 years

    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?

  • bogdanbujdea
    bogdanbujdea almost 7 years
    worked for me but only after I changed LoadedBehavior to Manual