I have a MediaElement where the source is bound to some data
What is the simplest way
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...
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 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!
Adding mediaElement.Position = new TimeSpan(0,0,1)
to the media player method doesn't (before playing the media) have any effect rather as suggested by MasterMastic and Guido Zanon. I created MediaElement event for MediaEnded with the given content and it worked.