■ 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 |
<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="MediaElement 클래스 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10 10 10 10"> <Grid.RowDefinitions> <RowDefinition Height="100*" /> <RowDefinition Height="10" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <Border Grid.Row="0" BorderBrush="Black" BorderThickness="1"> <MediaElement x:Name="mediaElement" LoadedBehavior="Manual" UnloadedBehavior="Manual" ScrubbingEnabled="True"/> </Border> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center"> <Button x:Name="openButton" Width="100" Margin="0,0,10,0" Content="열기" /> <Button x:Name="playButton" Width="100" Margin="0,0,10,0" Content="재생" /> <Button x:Name="stopButton" Width="100" Margin="0,0,10,0" Content="중단" /> </StackPanel> </Grid> </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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using Microsoft.Win32; using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.openButton.Click += openButton_Click; this.playButton.Click += playButton_Click; this.stopButton.Click += stopButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 열기 버튼 클릭시 처리하기 - openButton_Click(sender, e) /// <summary> /// 열기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "미디어 소스"; openFileDialog.Filter = "미디어 파일|*.avi;*.wmv"; if(openFileDialog.ShowDialog(this).GetValueOrDefault() == true) { this.mediaElement.Source = new Uri(openFileDialog.FileName); this.mediaElement.Pause(); } } #endregion #region 재생 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// 재생 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Play(); } #endregion #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 중단 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Stop(); } #endregion } } |