7

I'm trying to get video's to repeat forever using MediaElement. I found the code below at http://msdn.microsoft.com/en-us/library/ms741866.aspx and it works fine.

<!-- The MediaElement control plays the sound. -->
        <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="C:\MyVideo1.wmv" Storyboard.TargetName="myMediaElement"  
                                                RepeatBehavior="Forever" />

                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </MediaElement.Triggers>
        </MediaElement>

The problem I'm having is when I try to bind the MediaTimeLine to a xml source I get the error - "Must Specify URI".

<MediaTimeline Source="{Binding XPath=MyVideos}" 
     Storyboard.TargetName="myMediaElement" RepeatBehavior="Forever" />

Is there a C# solution that could replace the xaml?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
KenS
  • 71
  • 2
  • I removed the triggers and MediaTimeLine and created a xpath binding source for "myMediaElement". I then add this code to myMediaElement_MediaEnded: this.myMediaElement.Position = TimeSpan.FromSeconds(0); this.myMediaElement.Play(); It works but is slow to load for some reason – KenS Mar 08 '10 at 20:13
  • I'm also wondering how to do this using XAML. The problem seems to be that Source is expecting a URI, not a string. Binding to a string and automatically converting it to a URI seems to be kind of tricky, though, as demonstrated here: http://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding – Jakob Sep 04 '10 at 23:37
  • There goes 50 of my reputation with the question still unanswered. :( – Jakob Sep 11 '10 at 23:38

1 Answers1

1

Why don't you use a value converter? I know it's not fully WPF i.e. some C# coding is required, but this seems to do exactly what you want and need. WPF Tutorials has a pretty decent explanation so if you don't mind me referring you to that page then: http://www.wpftutorial.net/ValueConverters.html

BadGuy
  • 31
  • 4