I have a MediaElement where the source is bound to some data
<MediaElement Source='{Binding Something}' />
What is the simplest way to have the video repeat? Ideally, MediaElement would have a repeat behavior property.
<MediaElement RepeatBehavior='Forever' ... />
But I can't find such a property.
You need to add a Storyboard to the MediaElement. See the example below:
<MediaElement Name="myMediaElement" >
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<!-- The MediaTimeline has a RepeatBehavior="Forever" which makes the media play
over and over indefinitely.-->
<MediaTimeline Source="media\tada.wav" Storyboard.TargetName="myMediaElement"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
I made it work by setting the UnloadedBehavior
to MediaState
.Manual
and the following code:
private void mediaElement_OnMediaEnded(object sender, RoutedEventArgs e)
{
mediaElement.Position = new TimeSpan(0,0,1);
mediaElement.Play();
}
Setting the position to Zero didnt work...
I know it's a little late, but I couldn't get to work the example from MSDN. So after research I've found this project: WPF Media Kit, just click at the Browse link in the Latest Version at the right side of the screen. You'll enter the code of the sample application. I liked this library because a loop was as simple as:
MediaUriElement MyPlayer.Loop = True;
or
<DirectShowControls:MediaUriElement x:Name="MyPlayer" Loop="True" />
Hope this helps someone else.
Regards!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With