WPF 实现视频循环播放
方法1:后台代码处理MediaEnded事件
xaml:
<MediaElement Name="player" MediaEnded="player_MediaEnded" Source="Videos/test.wmv" />
cs:
private void player_MediaEnded(object sender, RoutedEventArgs e)
{
//设置一下视频进度,确保从头开始播放
player.Position = TimeSpan.Zero;
player.Play();
}
方法2:xaml中处理
MediaElement.Source 不需要赋值
使用触发器播放动画,设置动画的RepeatBehavior="Forever"即可实现循环播放
<MediaElement Name="player">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline Source="Videos/test.wmv" Storyboard.TargetName="player" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
注:视频的属性设置如下: