Animated Video Window

image_pdfimage_print


   
  

<Window x:Class="SoundAndVideo.AnimatedVideoWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimatedVideoWindow" Height="450" Width="400">

  <Window.Resources>
    <Storyboard x:Key="MediaStoryboardResource">
      <MediaTimeline Storyboard.TargetName="media" Source="test.mpg" FillBehavior="HoldEnd"
                      RepeatBehavior="Forever"></MediaTimeline>
      <DoubleAnimation Storyboard.TargetName="media" Storyboard.TargetProperty="(MediaElement.RenderTransform).(RotateTransform.Angle)"
    To="360" Duration="0:0:2" RepeatBehavior="Forever"></DoubleAnimation>
    </Storyboard>
  </Window.Resources>

  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdPlay">
        <EventTrigger.Actions>
          <BeginStoryboard Name="MediaStoryboard" Storyboard="{StaticResource MediaStoryboardResource}"/>
        </EventTrigger.Actions>
      </EventTrigger>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdStop">
        <EventTrigger.Actions>
          <StopStoryboard BeginStoryboardName="MediaStoryboard"/>
        </EventTrigger.Actions>
      </EventTrigger>      
    </StackPanel.Triggers>

      <Button Name="cmdPlay">Play (Declarative)</Button>
      <Button Name="cmdStop">Stop (Declarative)</Button>
      <Button Click="cmdPlayCode_Click">Play (Code)</Button>
    <MediaElement Name="media" Grid.Row="1" Stretch="None" RenderTransformOrigin="0.5,0.5" >
      <MediaElement.RenderTransform>
        <RotateTransform></RotateTransform>
      </MediaElement.RenderTransform>
    </MediaElement>
  </StackPanel>

</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace SoundAndVideo
{
    public partial class AnimatedVideoWindow : System.Windows.Window
    {
        public AnimatedVideoWindow()
        {
            InitializeComponent();
        }
        private void cmdPlayCode_Click(object sender, RoutedEventArgs e)
        {
            MediaTimeline timeline = new MediaTimeline(new Uri("test.mpg", UriKind.Relative));            
            timeline.RepeatBehavior = RepeatBehavior.Forever;

            MediaClock clock = timeline.CreateClock();
            MediaPlayer player = new MediaPlayer();
            player.Clock = clock;

            VideoDrawing videoDrawing = new VideoDrawing();
            videoDrawing.Rect = new Rect(150, 0, 100, 100);
            videoDrawing.Player = player;

            DrawingBrush brush = new DrawingBrush(videoDrawing);
            this.Background = brush;

            clock.Controller.Begin();
        }
    }
}