■ 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 79 80 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MediaElement 클래스 : 동영상 재생하기" Width="800" Height="600" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <MediaElement x:Name="mediaElement" Grid.Row="0" Grid.Column="0" Margin="5" ScrubbingEnabled="True" LoadedBehavior="Manual" Source="Sample/minions.mp4" /> <ScrollBar Name="positionScrollBar" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" Visibility="Hidden" /> <StackPanel Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" Orientation="Horizontal"> <StackPanel.Resources> <Style TargetType="Button"> <Setter Property="Margin" Value="2" /> </Style> </StackPanel.Resources> <Button x:Name="playButton"> <Image Source="Image/play.png" /> </Button> <Button x:Name="pauseButton" IsEnabled="False" Opacity="0.5"> <Image Source="Image/pause.png" /> </Button> <Button x:Name="stopButton"> <Image Source="Image/stop.png" /> </Button> <Button x:Name="restartButton"> <Image Source="Image/restart.png"/> </Button> <Button Name="previousButton"> <Image Source="Image/previous.png" /> </Button> <Button x:Name="nextButton"> <Image Source="Image/next.png" /> </Button> <Label Width="5" /> <Button Name="slowerButton"> <Image Source="Image/slower.png" /> </Button> <Button x:Name="fasterButton"> <Image Source="Image/faster.png" /> </Button> <Label Margin="5 0 0 0" VerticalAlignment="Center" Content="위치 :" /> <TextBox Name="positionTextBox" Width="35" Height="25" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="0.0" /> <Button Name="setPositionButton" Margin="5 0 0 0" Padding="5" Height="30" 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
using System; using System.Windows; using System.Windows.Threading; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디스패처 타이머 /// </summary> private DispatcherTimer dispatcherTimer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.mediaElement.MediaOpened += mediaElement_MediaOpened; this.playButton.Click += playButton_Click; this.pauseButton.Click += pauseButton_Click; this.stopButton.Click += stopButton_Click; this.restartButton.Click += restartButton_Click; this.previousButton.Click += previousButton_Click; this.nextButton.Click += nextButton_Click; this.fasterButton.Click += fasterButton_Click; this.slowerButton.Click += slowerButton_Click; this.setPositionButton.Click += setPositionButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.dispatcherTimer = new DispatcherTimer(); this.dispatcherTimer.Interval = TimeSpan.FromSeconds(0.1d); this.dispatcherTimer.Tick += dispatcherTimer_Tick; this.mediaElement.Stop(); } #endregion #region 디스패처 타이머 틱 처리하기 - dispatcherTimer_Tick(sender, e) /// <summary> /// 디스패처 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dispatcherTimer_Tick(object sender, EventArgs e) { ShowPosition(); } #endregion #region 미디어 엘리먼트 미디어 오픈시 처리하기 - mediaElement_MediaOpened(sender, e) /// <summary> /// 미디어 엘리먼트 미디어 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mediaElement_MediaOpened(object sender, RoutedEventArgs e) { this.positionScrollBar.Minimum = 0; this.positionScrollBar.Maximum = this.mediaElement.NaturalDuration.TimeSpan.TotalSeconds; this.positionScrollBar.Visibility = Visibility.Visible; } #endregion #region Play 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// Play 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Play(); SetButtnsEnabled(true); } #endregion #region Pause 버튼 클릭시 처리하기 - pauseButton_Click(sender, e) /// <summary> /// Pause 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pauseButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Pause(); SetButtnsEnabled(false); } #endregion #region Stop 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// Stop 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Stop(); SetButtnsEnabled(false); ShowPosition(); } #endregion #region Restart 버튼 클릭시 처리하기 - restartButton_Click(sender, e) /// <summary> /// Restart 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void restartButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Stop(); this.mediaElement.Play(); SetButtnsEnabled(true); } #endregion #region Previous 버튼 클릭시 처리하기 - previousButton_Click(sender, e) /// <summary> /// Previous 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void previousButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Position -= TimeSpan.FromSeconds(10); ShowPosition(); } #endregion #region Next 버튼 클릭시 처리하기 - nextButton_Click(sender, e) /// <summary> /// Next 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void nextButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.Position += TimeSpan.FromSeconds(10); ShowPosition(); } #endregion #region Faster 버튼 클릭시 처리하기 - fasterButton_Click(sender, e) /// <summary> /// Faster 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fasterButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.SpeedRatio *= 1.5d; } #endregion #region Slower 버튼 클릭시 처리하기 - slowerButton_Click(sender, e) /// <summary> /// Slower 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void slowerButton_Click(object sender, RoutedEventArgs e) { this.mediaElement.SpeedRatio /= 1.5d; } #endregion #region 위치 설정 버튼 클릭시 처리하기 - setPositionButton_Click(sender, e) /// <summary> /// 위치 설정 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void setPositionButton_Click(object sender, RoutedEventArgs e) { TimeSpan timespan = TimeSpan.FromSeconds(double.Parse(this.positionTextBox.Text)); this.mediaElement.Position = timespan; ShowPosition(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 위치 보여주기 - ShowPosition() /// <summary> /// 위치 보여주기 /// </summary> private void ShowPosition() { this.positionScrollBar.Value = this.mediaElement.Position.TotalSeconds; this.positionTextBox.Text = this.mediaElement.Position.TotalSeconds.ToString("0.0"); } #endregion #region 버튼들 이용 가능 여부 설정하기 - SetButtnsEnabled(isPlaying) /// <summary> /// 버튼들 이용 가능 여부 설정하기 /// </summary> /// <param name="isPlaying">재생 여부</param> private void SetButtnsEnabled(bool isPlaying) { if(isPlaying) { this.playButton.IsEnabled = false; this.pauseButton.IsEnabled = true; this.playButton.Opacity = 0.5d; this.pauseButton.Opacity = 1.0d; } else { this.playButton.IsEnabled = true; this.pauseButton.IsEnabled = false; this.playButton.Opacity = 1.0d; this.pauseButton.Opacity = 0.5d; } this.dispatcherTimer.IsEnabled = isPlaying; } #endregion } } |