Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Creating a simple seeking media player with MediaElement

$
0
0

Hello,

I'm trying to create a simple media player that supports seeking of the loaded media. I'm using creating a MediaTimeline and MediaClock in code-behind and trying to use the Controller's Seek method. It does not work. On MediaOpened i use Controller.Pause(), but i dont know if it does anything since the MediaElement is defined with LoadedBehavior set to Manual. Below is the application.

 

What am i doing wrong?

thanks

--oleg

 

MainWindow.xaml

<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"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><MediaElement x:Name="mediaElement" Width="320" Height="240" Margin="4" LoadedBehavior="Manual"/><Slider x:Name="slider" Grid.Row="1" Margin="4"/></Grid></Window>

MainWindow.xaml.cs

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) =>
                 {
                   slider.Maximum = mediaElement.NaturalDuration.TimeSpan.Seconds;
                   mediaElement.Clock.Controller.Pause();
                 };

      slider.ValueChanged += (o, e) =>
                  {
                    mediaElement.Clock.Controller.Seek(TimeSpan.FromSeconds(slider.Value), TimeSeekOrigin.BeginTime);
                  };

    }
  }
}

 


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>