Replaying a video continuously in a WPF media elem

2019-06-08 03:45发布

问题:

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?

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


回答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



回答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.



回答4:

I don't know for which reason but for me this never worked:

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

My gif stops playing after first iteration.

I tried to do different thing and looks like I found easy solution:

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

Code:

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

Looks like if you will set position as zero something wrong might happen. 'Play' method might stops working.