title: Creating a simple seeking media player with MediaElement in WPF
body: <p>I have also posted this on MSDN forums - i hope its not a problem.</p>
  
  <p>I am basically trying to create a WPF based video player which allows you to seek within media. I'm trying to implement it using MediaTimeline (i know i can change the Position property, but i had other issues which i'll post in a separate question). XAML and code-behind are below.</p>
  
  <p>Thanks for looking...</p>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" SizeToContent="WidthAndHeight"&gt;
    &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height="Auto"/&gt;
        &lt;RowDefinition Height="Auto"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;MediaElement x:Name="mediaElement" Width="320" Height="240" Margin="4" LoadedBehavior="Manual"/&gt;
      &lt;Slider x:Name="slider" Grid.Row="1" Margin="4"/&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  
  namespace WpfApplication5
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              var tl = new MediaTimeline(new Uri(@"c:\temp\!numa.wmv"));
  
              mediaElement.Clock = tl.CreateClock(true) as MediaClock;
  
  
              mediaElement.MediaOpened += (o, e) =&gt;
              {
                  slider.Maximum = mediaElement.NaturalDuration.TimeSpan.Seconds;
                  mediaElement.Clock.Controller.Pause();
              };
  
              slider.ValueChanged += (o, e) =&gt;
              {
                  mediaElement.Clock.Controller.Seek(TimeSpan.FromSeconds(slider.Value), TimeSeekOrigin.BeginTime);
              };
  
          }
      }
  }
  </code></pre>