■ MediaElement 엘리먼트에서 스토리보드를 사용해 제어하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" Background="Black" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <MediaElement Name="mediaElement" Width="600" Height="400" Stretch="Fill" /> <StackPanel HorizontalAlignment="Center" Margin="0 10 0 0" Orientation="Horizontal"> <Image Name="playImage" Source="IMAGE\play.png" /> <Image Name="pauseImage" Margin="10 0 0 0" Source="IMAGE\pause.png" /> <Image Name="resumeImage" Margin="10 0 0 0" Source="IMAGE\resume.png" /> <Image Name="stopImage" Margin="10 0 0 0" Source="IMAGE\stop.png" /> </StackPanel> <Slider Name="timelineSlider" HorizontalAlignment="Center" Margin="0 10 0 0" Width="250" /> <StackPanel.Triggers> <EventTrigger SourceName="playImage" RoutedEvent="Image.MouseDown"> <EventTrigger.Actions> <BeginStoryboard Name= "beginStoryboard"> <Storyboard SlipBehavior="Slip"> <MediaTimeline x:Name="mediaTimeline" Source="MEDIA\sample.avi" Storyboard.TargetName="mediaElement" BeginTime="00:00:00" Duration="00:00:30" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger SourceName="pauseImage" RoutedEvent="Image.MouseDown"> <EventTrigger.Actions> <PauseStoryboard BeginStoryboardName="beginStoryboard" /> </EventTrigger.Actions> </EventTrigger> <EventTrigger SourceName="resumeImage" RoutedEvent="Image.MouseDown"> <EventTrigger.Actions> <ResumeStoryboard BeginStoryboardName="beginStoryboard" /> </EventTrigger.Actions> </EventTrigger> <EventTrigger SourceName="stopImage" RoutedEvent="Image.MouseDown"> <EventTrigger.Actions> <StopStoryboard BeginStoryboardName="beginStoryboard" /> </EventTrigger.Actions> </EventTrigger> </StackPanel.Triggers> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.mediaElement.MediaOpened += mediaElement_MediaOpened; this.mediaTimeline.CurrentTimeInvalidated += mediaTimeline_CurrentTimeInvalidated; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 미디어 엘리먼트 미디어 오픈시 처리하기 - mediaElement_MediaOpened(sender, e) /// <summary> /// 미디어 엘리먼트 미디어 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mediaElement_MediaOpened(object sender, EventArgs e) { this.timelineSlider.Maximum = this.mediaElement.NaturalDuration.TimeSpan.TotalMilliseconds; } #endregion #region 미디어 타임라인 현재 시간 무효화시 처리하기 - mediaTimeline_CurrentTimeInvalidated(sender, e) /// <summary> /// 미디어 타임라인 현재 시간 무효화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mediaTimeline_CurrentTimeInvalidated(object sender, EventArgs e) { this.timelineSlider.Value = this.mediaElement.Position.TotalMilliseconds; } #endregion } } |