Play a Media File : MediaElement « Windows Presentation Foundation « C# / C Sharp






Play a Media File

Play a Media File
  
<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);
        }
    }
}

   
    
  








Related examples in the same category

1.Using MediaElement for AudioUsing MediaElement for Audio
2.Overlapping Videos with EffectsOverlapping Videos with Effects
3.Simple Media PlayerSimple Media Player
4.Play mp3 filePlay mp3 file
5.Play wav filePlay wav file
6.Declarative PlaybackDeclarative Playback
7.Sound And Video Playback in CodeSound And Video Playback in Code
8.Play with MediaPlayerPlay with MediaPlayer
9.Play Audio AsynchronouslyPlay Audio Asynchronously
10.Play System SoundsPlay System Sounds