Widgets How to Make a Media Element Loop Indefinitely


I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Go Daddy Deal of the Week: 30% off your order at! Offer expires 11/6/12

Recent comments




Code Project Associate Logo


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

How to Make a Media Element Loop Indefinitely

I was surprised to see that the XAML media element did not have a loop property built into it. Surely having a clip loop is basic functionality? In any case I came up with a JavaScript solution that seemed much simpler to implement than the pure XAML solution offered on the MSDN website

JavaScript Solution

    handleLoad: function(control, userContext, rootElement)
        this.control = control;

        //  Get a reference to your media element (mine is called "MainMovie")         = control.content.findName("MainMovie");
        // Add an Event Listener for the "MediaEnded" Event"MediaEnded", Silverlight.createDelegate(this,this.movieMediaEnded));

    //When the end of the movie is reached, return the movie to the start and play it again
    movieMediaEnded: function(sender, eventArgs)
        sender.Position = "00:00:00";;

See my solution in action at The Daily Prophet Online.

MSDN's Pure XAML Solution

<Page xmlns=""

xmlns:x="" >


<!-- The MediaElement control plays the sound. -->

<MediaElement Name="myMediaElement" >


<EventTrigger RoutedEvent="MediaElement.Loaded">




<!-- 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" />









Posted by Williarob on Tuesday, October 30, 2007 12:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed
blog comments powered by Disqus