Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is simplest way to repeat a video in a MediaElement

Tags:

wpf

video

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.

like image 938
user380719 Avatar asked Aug 04 '10 14:08

user380719


3 Answers

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>
like image 196
Wouter Janssens Avatar answered Oct 25 '22 21:10

Wouter Janssens


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

like image 25
Guido Zanon Avatar answered Oct 25 '22 19:10

Guido Zanon


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!

like image 43
BlackCath Avatar answered Oct 25 '22 20:10

BlackCath