Replaying a video continuously in a WPF media element

青春壹個敷衍的年華 提交于 2019-12-07 21:03:05

问题


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



来源:https://stackoverflow.com/questions/20000806/replaying-a-video-continuously-in-a-wpf-media-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!