Feb 29, 2008

Media tracking in Silverlight

During the past few days I spent quite some time looking into the best and possibly most robust way to track audio and video content in Silverlight. The objective is simple: every time a video or an audio clip is loaded, a timeline is to appear and is to give users the ability to play/pause/stop the clip, see its progress, mute it, or simply seek parts in it.

The toughest part of all is creating the progress bar... well, not so much creating it, but making it work. As with most controls created by Microsoft, hooking into the right event is always the way to go. In a similar way, media tracking can be done by setting markers in the media content at certain intervals and then catching the MarkerReached event when those markers are being hit. Unfortunately, modifying the markers through .NET code was not as easy as it seems... apparently Silverlight has issues when markers are being added to a MediaElement. The result of that is a nice crash of the browser.

Luckily, media markers can be embedded through the Expression Encoder. To save time, avoid carpal tunnel syndrome, and spare some nerves, there is an easy way to automate the process of setting the markers into the media file. The steps are:
  1. Get the total duration time of the media content.

  2. Set your step interval (i.e. the time between 2 consecutive markers).

  3. Create an XML file of the format
    <Markers>
    <Marker Time="00:00:01"
    Value="Marker1"
    GenerateKeyFrame="True"
    GenerateThumbnail="False" />
    ...
    </Markers>

  4. Import the XML file into the Markers section in Expression Encoder

  5. Encode the media content.

No comments: