Thursday, August 13, 2009

Play a seamless loop of video in Silverlight


How to play a video in a loop in Silverlight?
On numerous websites, a solution using MediaEnded event is offered.
That solution does not work because there's a visible pause before restart.


Here's one with a Marker. It's still not perfect because if you put the time of the marker too close to the end, it may not be reached. For example, 0.01 seconds did not work.


If you make it too far, the video will not be seamless. But at least, it's better than the MediaEnded way of doing it.


MediaElement.MediaOpened += delegate
{
MediaElement.Markers.Add(new TimelineMarker
{
Time = MediaElement.NaturalDuration.TimeSpan.Subtract(TimeSpan.FromSeconds(0.1))
});
MediaElement.MarkerReached += delegate
{
MediaElement.Stop();
MediaElement.Play();
};
};

Note that the marker needs to be set after opening the media file.

No comments:

Post a Comment