
Timeline Markers (Synchronization Points)
A timeline marker is metadata associated with a particular point in a media file. These markers are usually created ahead of time and stored in the media file itself. They are typically used to name different scenes in a video, enable users to seek to a selected position, or provide scripting cues.
When the MediaElement reaches a timeline marker during playback, it raises the MarkerReached event. Your code can handle this event and use timeline markers to trigger actions. A MediaElement object's Markers property enables you to access the header-embedded markers stored in the currently playing media file. You can also use this property to add new timeline markers.
Although the MediaElement object supports all Windows media technology markers (markers, metadata script commands, and separate-stream script commands), the collection returned from the Markers property does not include any timeline markers encoded as a separate stream in the media file, such as separate-stream script commands. If a file contains both header-embedded script commands and separate-stream script commands, this collection contains only those commands that are embedded in the file header. Separate-stream script commands trigger the MarkerReached event when encountered during media playback. However, these markers cannot be accessed ahead of time by using the Markers property.
MediaElement uses TimelineMarker objects to represent the collection of markers returned by the Markers property. The TimelineMarker object provides the following properties, which describe its time, name, and value:
Time: A TimeSpan structure that specifies the time when the marker is reached.
Type: A string that specifies the marker's type. This value can be any user-defined string.
Text: A string that specifies the marker's value. This value can be any user-defined string.
The following example creates a MediaElement and registers for the MarkerReached event. It also creates several TextBlock objects.
<StackPanel Margin="40">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Foreground="DarkGray">Time:</TextBlock>
<TextBlock x:Name="timeTextBlock" FontSize="12" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Foreground="DarkGray">Type:</TextBlock>
<TextBlock x:Name="typeTextBlock" FontSize="12" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Foreground="DarkGray">Value:</TextBlock>
<TextBlock x:Name="valueTextBlock" FontSize="12" />
</StackPanel>
<!-- The MediaElement has the MarkerReached event attached. -->
<MediaElement MarkerReached="OnMarkerReached" HorizontalAlignment="Left"
Source="thebutterflyandthebear.wmv" Width="300" Height="200" />
</StackPanel>
The accompanying code handles the MarkerReached event. It retrieves the Time, Type, and Text values of the timeline marker that triggered the event and displays that information in the TextBlock objects declared in the preceding markup.
Private Sub OnMarkerReached(ByVal sender As Object, ByVal e As TimelineMarkerRoutedEventArgs)
timeTextBlock.Text = e.Marker.Time.Seconds.ToString
typeTextBlock.Text = e.Marker.Type.ToString
valueTextBlock.Text = e.Marker.Text.ToString
End Sub
private void OnMarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
{
timeTextBlock.Text = e.Marker.Time.Seconds.ToString();
typeTextBlock.Text = e.Marker.Type.ToString();
valueTextBlock.Text = e.Marker.Text.ToString();
}
Defining Media Markers
There are two ways to create media markers. The first is to use an editor such as the Windows Media File Editor (installed as part of the Windows Media Encoder 9 installation) or Microsoft Expression Media Encoder to create markers and save them in the media file itself or in a separate stream.
The second approach is to dynamically generate new TimelineMarker objects and add them to the MediaElement through its Markers property. These timeline markers are temporary and are not saved in the media file. Any such timeline markers you create are lost when a new media file is loaded by the MediaElement.