Subtitles in a MediaElement

16 Nov

Loading subtitles to MediaElement in Silverlight is pretty easy but there are some features that you need to be aware of.

You associate a list of subtitles to a MediaElement by adding Markers to the MediaElement.

<MediaElement
    x:Name="MyMediaElement" />

MyMediaElement.Markers.Add(
    new TimelineMarker()
    {
        Text = "xamlgeek made this subtitle",
        Time = TimeSpan.FromMilliseconds(1000)
    });

To display the subtitle on an UI element like a formatted TextBlock you need to subscribe to the MarkerReached event on the MediaElement. This event will be fired when the media reaches the marked on the specified timespan.

MyMediaElement.MarkerReached += (se, ea) =>
    MySubTitleTextBlock = ea.Marker.Text;

But there is one minor problem that you need to be aware of when building more complex media applications. If you add a TimeLineMarker to a MediaElement before the MediaOpened event is fired, the Markers will not trigger the MarkerReached event.

You can take a look at http://sputnik.tv2.dk to see a running media application using subtitles as described above.

by xamlgeek

One Response to “Subtitles in a MediaElement”

  1. Anders February 11, 2012 at 16:06 #

    If you want a 1-month free access to Sputnik – you can get it her:
    http://www.tv-pakke.dk/220/gratis-sputnik-i-en-maaned-laes-her-hvordan/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: