在 WPF 媒体元素中连续重播视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20000806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replaying a video continuously in a WPF media element
提问by Anoushka Seechurn
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?
但是,以上不会重放视频文件。为什么?我做错了什么?
回答by vivat pisces
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.
Play() 从当前位置开始,因此您必须先到起始位置,然后再播放。
So you have to reset the position before replaying:
所以你必须在重播之前重置位置:
me.Position = TimeSpan.FromSeconds(0);
me.Play();
回答by tpartee
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.
您还可以使用 Storyboard 来控制和循环视频,而无需挂钩事件或执行任何代码隐藏工作。这是 MSDN 上推荐的解决方案,是 MVVM 设计更优雅、更合适的解决方案。
Read more on MSDN here, includes examples:
在此处阅读 MSDN 上的更多信息,包括示例:
http://msdn.microsoft.com/en-us/library/ms741866%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/ms741866%28v=vs.100%29.aspx
回答by Brent Bradford
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.
希望这能回答你的问题。
回答by Ievgen Naida
I don't know the reason but this never workedfor me with a GIF:
我不知道原因,但这对我来说从来没有用过GIF:
me.Position = TimeSpan.FromSeconds(0);
me.Play();
My GIF stops playing after the first iteration.
我的 GIF 在第一次迭代后停止播放。
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.
使用 1 毫秒,而 0 毫秒可能会停止播放,您无法使用“播放”方法再次运行它。

