<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="450" Width="300">
<StackPanel x:Name="Panel">
<StackPanel.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="btnPlay">
<EventTrigger.Actions>
<BeginStoryboard Name="ClockStoryboard">
<Storyboard x:Name="Storyboard" SlipBehavior="Slip"
CurrentTimeInvalidated="Storyboard_Changed">
<MediaTimeline BeginTime="0" Source="c:\a.avi"
Storyboard.TargetName="meMediaElement"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="btnPause">
<EventTrigger.Actions>
<PauseStoryboard BeginStoryboardName="ClockStoryboard" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="btnResume">
<EventTrigger.Actions>
<ResumeStoryboard BeginStoryboardName="ClockStoryboard" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="btnStop">
<EventTrigger.Actions>
<StopStoryboard BeginStoryboardName="ClockStoryboard" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Slider.PreviewMouseLeftButtonDown"
SourceName="sldPosition" >
<PauseStoryboard BeginStoryboardName="ClockStoryboard" />
</EventTrigger>
<EventTrigger RoutedEvent="Slider.PreviewMouseLeftButtonUp"
SourceName="sldPosition" >
<ResumeStoryboard BeginStoryboardName="ClockStoryboard" />
</EventTrigger>
</StackPanel.Triggers>
<MediaElement Name="meMediaElement" HorizontalAlignment="Center"
Margin="5" MinHeight="300" Stretch="Fill"
MediaOpened="MediaOpened" />
<Button Content="_Play" Name="btnPlay" />
<Button Content="P_ause" Name="btnPause" />
<Button Content="_Resume" Name="btnResume" />
<Button Content="_Stop" Name="btnStop" />
<Slider HorizontalAlignment="Center" Margin="5"
Name="sldPosition" Width="250"
ValueChanged="sldPosition_ValueChanged">
</Slider>
<TextBlock Text="Volume:"/>
<Slider Minimum="0" Maximum="1" TickFrequency="0.1" TickPlacement="TopLeft" Value="{Binding ElementName=meMediaElement, Path=Volume, Mode=TwoWay}" />
<TextBlock Text="Balance:"/>
<Slider Minimum="-1" Maximum="1" TickFrequency="0.2" TickPlacement="TopLeft" Value="{Binding ElementName=meMediaElement, Path=Balance, Mode=TwoWay}" />
</StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
public partial class Window1 : Window
{
bool ignoreValueChanged = false;
public Window1()
{
InitializeComponent();
}
private void MediaOpened(object sender, EventArgs e)
{
sldPosition.Maximum = meMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
}
private void Storyboard_Changed(object sender, EventArgs e)
{
ClockGroup clockGroup = sender as ClockGroup;
MediaClock mediaClock = clockGroup.Children[0] as MediaClock;
if (mediaClock.CurrentProgress.HasValue){
ignoreValueChanged = true;
sldPosition.Value = meMediaElement.Position.TotalMilliseconds;
ignoreValueChanged = false;
}
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e){
if (ignoreValueChanged)
{
return;
}
Storyboard.Seek(Panel,TimeSpan.FromMilliseconds(sldPosition.Value),TimeSeekOrigin.BeginTime);
}
}
}