10

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.

user380719
  • 9,663
  • 15
  • 54
  • 89

4 Answers4

20

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>
Wouter Janssens
  • 1,593
  • 10
  • 17
  • 1
    FWIW this method would start to bog down after repeating the video for 1-3 minutes. I think this opens the video file each time through. I used the method from this other answer and it loops forever without slowdowns/freezing: https://stackoverflow.com/a/5975876/31307 – goldenratio Nov 12 '19 at 20:22
  • @goldenratio yes tested it as well, and after a while it doesn't repeat anymore, and the app gets sluggish, so it is obviously some kind of resource leak – Alexander Gräf Aug 30 '22 at 19:10
17

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

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
Guido Zanon
  • 2,939
  • 26
  • 31
  • 6
    I came to this question in-order to answer the very same answer so I must mention that setting the position to `TimeSpan.Zero` works out just fine. – MasterMastic May 02 '13 at 06:07
  • 1
    I tested my self and find that is working fine with TimeSpan.Zero. Nice Ken – akatran Nov 19 '13 at 13:25
  • +MasterMastic TimeSpan.Zero not working for all cases. – Ievgen Oct 21 '14 at 13:26
  • 1
    This and TimeSpan.Zero worked for me the best. The trigger was prematurely rewinding the video in the loop and wouldn't play the video to the end. – ScottN Jan 04 '15 at 22:42
  • 4
    Remove `mediaElement.Play();` if you have `LoadedBehaviour` set to `Play`. – lionello Feb 28 '15 at 10:38
  • Also works for gif graphics. I hate that there is no easy and reliable way built in. – CodingYourLife Jul 26 '16 at 09:42
  • `TimeSpan.Zero` created an annoying flicker effect every time the video restarted; setting it to `new TimeSpan(0, 0, 0, 0, 10)` (10ms) worked for me. – Sasino Dec 23 '20 at 21:20
3

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!

Henry Kwon
  • 101
  • 1
  • 3
  • 12
BlackCath
  • 816
  • 13
  • 23
0

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.

Blue
  • 22,608
  • 7
  • 62
  • 92
A. Lartey
  • 59
  • 2