This documentation is archived and is not being maintained.

MediaTimeline Class

Provides a Timeline for media content.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public class MediaTimeline : Timeline, 
<MediaTimeline .../>

MediaTimeline is a Timeline object which provides control over media timing in the same way that animation timeline objects control animations. For example, a MediaTimeline has associated Duration and BeginTime properties can be used to specify when media begins and how long it plays. See Animation Overview for more information on animation timelines.

There are two ways to associate a Timeline to a MediaElement using a MediaTimeline.

  1. Inside of a Storyboard, when a MediaTimeline is targets a MediaElement, a MediaClock will be created and assigned to the MediaElement’s associated player. See How to: Control a MediaElement by Using a Storyboard for an example;

  2. By explicitly creating a MediaClock from a MediaTimeline and assigning it to a MediaElement.

If the Duration of the MediaTimeline is set to Automatic (default), the duration of MediaTimeline is the natural duration of the media source. To find the natural duration of the media source programmatically, query the NaturalDuration property of the MediaElement.

This example shows how to control a MediaElement by using a MediaTimeline in a Storyboard.

When you use a MediaTimeline in a Storyboard to control the timing of a MediaElement, the functionality is identical to the functionality of other Timeline objects, such as animations. For example, a MediaTimeline uses Timeline properties like the BeginTime property to specify when to start a MediaElement (start media playback). It also uses the Duration property to specify how long the MediaElement is active (duration of media playback). For more information about using Timeline objects with a Storyboard, see Storyboards Overview.

This example shows how to create a simple media player that uses a MediaTimeline to control playback. The media player includes play, pause, resume, and stop buttons. The player also has a Slider control that acts as a progress bar.

The following example creates the user interface (UI) for the media player.

<Page xmlns=""
  x:Class="SDKSample.MediaTimelineExample" >

  <StackPanel Background="Black">

    <MediaElement Name="myMediaElement" MediaOpened="Element_MediaOpened"
     Width="260" Height="150" Stretch="Fill" />

    <!-- Button controls for play, pause, resume, and stop. -->
  <StackPanel HorizontalAlignment="Center" Width="260" Orientation="Horizontal">
    <Image Name="PlayButton" Source="images\UI_play.gif" Margin="30,10,10,10" />
    <Image Name="PauseButton" Source="images\UI_pause.gif" Margin="10" />
    <Image Name="ResumeButton" Source="images\UI_resume.gif" Margin="10" />
    <Image Name="StopButton" Source="images\UI_stop.gif" Margin="10" />

  <!-- Ths slider shows the progress of the media. -->
  <Slider Name="timelineSlider" Margin="5" Width="250" HorizontalAlignment="Center"/>

    <EventTrigger RoutedEvent="Image.MouseDown" SourceName="PlayButton">
        <BeginStoryboard Name= "myBegin">

          <Storyboard SlipBehavior="Slip">

            <!-- The MediaTimeline controls the timing of the video and acts like other Timeline objects.  
                 For example, although the video clip (numbers.wmv) lasts longer, playback ends after six  
                 seconds because that is the duration of the MediaTimeline (Duration="0:0:6"). -->
            <MediaTimeline Source="media\numbers.wmv" Storyboard.TargetName="myMediaElement"  
             BeginTime="0:0:0" Duration="0:0:6" CurrentTimeInvalidated="MediaTimeChanged" />


    <!-- These triggers impliment the functionality of the Pause, Resume
         and Stop buttons.-->
    <EventTrigger RoutedEvent="Image.MouseDown" SourceName="PauseButton">
        <PauseStoryboard BeginStoryboardName="myBegin" />
    <EventTrigger RoutedEvent="Image.MouseDown" SourceName="ResumeButton">
        <ResumeStoryboard BeginStoryboardName="myBegin" />
    <EventTrigger RoutedEvent="Image.MouseDown" SourceName="StopButton">
        <StopStoryboard BeginStoryboardName="myBegin" />


The following example creates the functionality for the progress bar.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SDKSample

    public partial class MediaTimelineExample : Page
        // When the media opens, initialize the "Seek To" slider maximum value
        // to the total number of miliseconds in the length of the media clip. 
        private void Element_MediaOpened(object sender, EventArgs e)
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;

        private void MediaTimeChanged(object sender, EventArgs e)
            timelineSlider.Value = myMediaElement.Position.TotalMilliseconds;


For the complete sample, see Media Gallery.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0