■ 웹 카메라를 사용하는 방법을 보여준다.
▶ MainPage.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 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 |
<Page x:Class="TestApplication.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestApplication" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.ChildrenTransitions> <TransitionCollection> <EntranceThemeTransition /> </TransitionCollection> </Grid.ChildrenTransitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Style="{StaticResource HeaderTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Center" IsHitTestVisible="false" TextWrapping="NoWrap" Text="웹 카메라 테스트" /> <StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Button x:Name="initializeVideoButton" HorizontalAlignment="Left" Margin="10" Width="180" Content="비디오 초기화" /> <Button x:Name="initializeAudioButton" Margin="10" Width="120" Content="오디오 초기화" /> <Button x:Name="closeCameraButton" Margin="10" Width="120" Content="카메라 닫기" /> </StackPanel> <StackPanel Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Button x:Name="takePhotoButton" Margin="10" Width="120" IsEnabled="False" Content="사진 찍기" /> <Button x:Name="recordVideoButton" Margin="10" Width="150" IsEnabled="False" Content="비디오 기록 시작" /> <Button x:Name="recordAudioButton" Margin="10" Width="150" IsEnabled="False" Content="오디오 기록 시작" /> </StackPanel> <StackPanel Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <StackPanel Margin="10"> <TextBlock Name="previewTextBlock" HorizontalAlignment="Center" Height="30" Margin="10" Text="미리 보기" /> <Canvas Name="previewCanvas" Width="300" Height="300" Background="Gray" Visibility="Visible"> <CaptureElement x:Name="previewCaptureElement" HorizontalAlignment="Left" Width="{Binding ElementName=previewCanvas, Path=ActualWidth}" Height="{Binding ElementName=previewCanvas, Path=ActualHeight}" Visibility="Visible" /> </Canvas> </StackPanel> <StackPanel Margin="10"> <TextBlock Name="lastCapturedPhotoTextBlock" HorizontalAlignment="Center" Margin="10" Height="30" Visibility="Visible" Text="마지막 촬영 사진" /> <Canvas Name="lastCapturedPhotoCanvas" Width="300" Height="300" Background="Gray" Visibility="Visible"> <Image x:Name="lastCapturedPhotoImage" Width="{Binding ElementName=lastCapturedPhotoCanvas, Path=ActualWidth}" Height="{Binding ElementName=lastCapturedPhotoCanvas, Path=ActualHeight}" Visibility="Visible" /> </Canvas> </StackPanel> <StackPanel Margin="10"> <TextBlock Name="lastRecordedVideoTextBlock" HorizontalAlignment="Center" Margin="10" Height="30" Visibility="Visible" Text="마지막 기록 비디오" /> <Canvas Name="lastRecordedVideoCanvas" Width="300" Height="300" Background="Gray" Visibility="Visible"> <MediaElement x:Name="lastRecordedVideoMediaElement" Width="{Binding ElementName=lastRecordedVideoCanvas, Path=ActualWidth}" Height="{Binding ElementName=lastRecordedVideoCanvas, Path=ActualHeight}" Visibility="Visible" /> </Canvas> </StackPanel> <StackPanel> <Canvas Margin="0" Width="0" Height="0"> <MediaElement x:Name="playbackMediaElement" Width="0" Height="0" /> </Canvas> </StackPanel> </StackPanel> <TextBlock Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Text="status" /> <TextBox x:Name="statusTextBlock" Grid.Row="5" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Height="100" Width="600" TextWrapping="Wrap" IsReadOnly="True" /> </Grid> </Page> |
▶ MainPage.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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
using System; using Windows.Media; using Windows.Media.Capture; using Windows.Media.MediaProperties; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; namespace TestApplication { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Private #region 액션 - Action /// <summary> /// 액션 /// </summary> private enum Action { /// <summary> /// 활성화 /// </summary> ENABLED, /// <summary> /// 비활성화 /// </summary> DISABLED } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 미디어 캡처 파일 /// </summary> private MediaCapture mediaCapture; /// <summary> /// 사진 파일명 /// </summary> private readonly string PHOTO_FILE_NAME = "photo.png"; /// <summary> /// 비디오 파일명 /// </summary> private readonly string VIDEO_FILE_NAME = "video.mp4"; /// <summary> /// 오디오 파일명 /// </summary> private readonly string AUDIO_FILE_NAME = "audio.mp3"; /// <summary> /// 사진 저장소 파일 /// </summary> private StorageFile photoStorageFile; /// <summary> /// 비디오 저장소 파일 /// </summary> private StorageFile videoStorageFile; /// <summary> /// 오디오 저장소 파일 /// </summary> private StorageFile audioStorageFile; /// <summary> /// 미리보기 여부 /// </summary> private bool isPreviewing; /// <summary> /// 레코딩 여부 /// </summary> private bool isRecording; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { this.InitializeComponent(); SetInitializeButtonVisibility(Action.ENABLED); SetVideoButtonVisibility(Action.DISABLED); SetAudioButtonVisibility(Action.DISABLED); this.isPreviewing = false; this.isRecording = false; this.initializeVideoButton.Click += initializeVideoButton_Click; this.initializeAudioButton.Click += initializeAudioButton_Click; this.closeCameraButton.Click += closeCameraButton_Click; this.takePhotoButton.Click += takePhotoButton_Click; this.recordVideoButton.Click += recordVideoButton_Click; this.recordAudioButton.Click += recordAudioButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 비디오 초기화 버튼 클릭시 처리하기 - initializeVideoButton_Click(sender, e) /// <summary> /// 비디오 초기화 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void initializeVideoButton_Click(object sender, RoutedEventArgs e) { SetInitializeButtonVisibility(Action.DISABLED); SetVideoButtonVisibility(Action.DISABLED); SetAudioButtonVisibility(Action.DISABLED); try { if(this.mediaCapture != null) { if(this.isPreviewing) { await this.mediaCapture.StopPreviewAsync(); this.lastCapturedPhotoImage.Source = null; this.lastRecordedVideoMediaElement.Source = null; this.isPreviewing = false; } if(isRecording) { await this.mediaCapture.StopRecordAsync(); this.isRecording = false; this.recordVideoButton.Content = "비디오 기록 시작"; this.recordAudioButton.Content = "오디오 기록 시작"; } this.mediaCapture.Dispose(); this.mediaCapture = null; } this.statusTextBlock.Text = "비디오 캡처를 위한 카메라 초기화 중..."; this.mediaCapture = new MediaCapture(); await this.mediaCapture.InitializeAsync(); this.statusTextBlock.Text = "비디오 기록을 위한 장치가 성공적으로 초기화되었습니다!"; this.mediaCapture.Failed += mediaCapture_Failed; this.mediaCapture.RecordLimitationExceeded += mediaCapture_RecordLimitExceeded; this.previewCaptureElement.Source = this.mediaCapture; await this.mediaCapture.StartPreviewAsync(); this.isPreviewing = true; this.statusTextBlock.Text = "카메라 미리보기가 성공했습니다."; SetVideoButtonVisibility(Action.ENABLED); this.initializeAudioButton.IsEnabled = true; } catch(Exception exception) { this.statusTextBlock.Text = "비디오 기록을 위한 카메라 초기화를 할 수 없습니다 : " + exception.Message; } } #endregion #region 오디오 초기화 버튼 클릭시 처리하기 - initializeAudioButton_Click(sender, e) /// <summary> /// 오디오 초기화 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void initializeAudioButton_Click(object sender, RoutedEventArgs e) { SetInitializeButtonVisibility(Action.DISABLED); SetVideoButtonVisibility(Action.DISABLED); SetAudioButtonVisibility(Action.DISABLED); try { if(this.mediaCapture != null) { if(this.isPreviewing) { await this.mediaCapture.StopPreviewAsync(); this.lastCapturedPhotoImage.Source = null; this.lastRecordedVideoMediaElement.Source = null; this.isPreviewing = false; } if(this.isRecording) { await this.mediaCapture.StopRecordAsync(); this.isRecording = false; this.recordVideoButton.Content = "비디오 기록 시작"; this.recordAudioButton.Content = "오디오 기록 시작"; } this.mediaCapture.Dispose(); this.mediaCapture = null; } this.statusTextBlock.Text = "오디오 캡처를 위한 카메라 초기화 중..."; this.mediaCapture = new MediaCapture(); MediaCaptureInitializationSettings mediaCaptureInitializationSettings = new MediaCaptureInitializationSettings(); mediaCaptureInitializationSettings.StreamingCaptureMode = StreamingCaptureMode.Audio; mediaCaptureInitializationSettings.MediaCategory = MediaCategory.Other; mediaCaptureInitializationSettings.AudioProcessing = AudioProcessing.Default; await this.mediaCapture.InitializeAsync(mediaCaptureInitializationSettings); this.statusTextBlock.Text = "오디오 기록을 위한 장치가 성공적으로 초기화되었습니다!" + "\n기록을 시작하기 위해 \'오디오 기록 시작\' 버튼을 누르시기 바랍니다."; this.mediaCapture.Failed += mediaCapture_Failed; this.mediaCapture.RecordLimitationExceeded += mediaCapture_RecordLimitExceeded; SetAudioButtonVisibility(Action.ENABLED); this.initializeVideoButton.IsEnabled = true; } catch(Exception exception) { this.statusTextBlock.Text = "오디오 기록을 위한 카메라 초기화를 할 수 없습니다 : " + exception.Message; } } #endregion #region 카메라 닫기 버튼 클릭시 처리하기 - closeCameraButton_Click(sender, e) /// <summary> /// 카메라 닫기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeCameraButton_Click(object sender, RoutedEventArgs e) { SetInitializeButtonVisibility(Action.DISABLED); SetVideoButtonVisibility(Action.DISABLED); SetAudioButtonVisibility(Action.DISABLED); ProcessCloseCamera(); } #endregion #region 미디어 캡처 실패시 처리하기 - mediaCapture_Failed(mediaCapture, e) /// <summary> /// 미디어 캡처 실패시 처리하기 /// </summary> /// <param name="mediaCapture">미디어 캡처</param> /// <param name="e">이벤트 인자</param> private async void mediaCapture_Failed(MediaCapture mediaCapture, MediaCaptureFailedEventArgs e) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { try { this.statusTextBlock.Text = "미디어 캡처 실패 : " + e.Message; if(this.isRecording) { await this.mediaCapture.StopRecordAsync(); this.statusTextBlock.Text += "\n 기록이 중단되었습니다."; } } catch(Exception) { } finally { SetInitializeButtonVisibility(Action.DISABLED); SetVideoButtonVisibility(Action.DISABLED); SetAudioButtonVisibility(Action.DISABLED); this.statusTextBlock.Text += "\n카메라가 연결되어 있는지 체크하시기 바랍니다. 앱을 다시 시작해 주시기 바랍니다."; } }); } #endregion #region 미디어 캡처 기록 제한 초과시 처리하기 - mediaCapture_RecordLimitExceeded(mediaCapture) /// <summary> /// 미디어 캡처 기록 제한 초과시 처리하기 /// </summary> /// <param name="mediaCapture">미디어 캡처</param> public async void mediaCapture_RecordLimitExceeded(MediaCapture mediaCapture) { try { if(this.isRecording) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { try { this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단하고 있습니다."; await this.mediaCapture.StopRecordAsync(); this.isRecording = false; this.recordVideoButton.Content = "비디오 기록 시작"; this.recordAudioButton.Content = "오디오 기록 시작"; if(this.mediaCapture.MediaCaptureSettings.StreamingCaptureMode == StreamingCaptureMode.Audio) { this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단했습니다 : " + this.audioStorageFile.Path; } else { this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단했습니다 : " + this.videoStorageFile.Path; } } catch(Exception exception) { this.statusTextBlock.Text = exception.Message; } }); } } catch(Exception exception) { this.statusTextBlock.Text = exception.Message; } } #endregion #region 사진 찍기 버튼 클릭시 처리하기 - takePhotoButton_Click(sender, e) /// <summary> /// 사진 찍기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void takePhotoButton_Click(object sender, RoutedEventArgs e) { try { this.takePhotoButton.IsEnabled = false; this.recordVideoButton.IsEnabled = false; this.lastCapturedPhotoImage.Source = null; this.photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, CreationCollisionOption.GenerateUniqueName); ImageEncodingProperties imageEncodingProperties = ImageEncodingProperties.CreatePng(); await this.mediaCapture.CapturePhotoToStorageFileAsync(imageEncodingProperties, this.photoStorageFile); this.takePhotoButton.IsEnabled = true; this.statusTextBlock.Text = "사진 찍기가 성공했습니다 : " + this.photoStorageFile.Path; IRandomAccessStream randomAccessStream = await this.photoStorageFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(randomAccessStream); this.lastCapturedPhotoImage.Source = bitmapImage; } catch(Exception exception) { this.statusTextBlock.Text = exception.Message; ProcessCloseCamera(); } finally { this.takePhotoButton.IsEnabled = true; this.recordVideoButton.IsEnabled = true; } } #endregion #region 비디오 기록 버튼 클릭시 처리하기 - recordVideoButton_Click(sender, e) /// <summary> /// 비디오 기록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void recordVideoButton_Click(object sender, RoutedEventArgs e) { try { this.takePhotoButton.IsEnabled = false; this.recordVideoButton.IsEnabled = false; this.lastRecordedVideoMediaElement.Source = null; if(this.recordVideoButton.Content.ToString() == "비디오 기록 시작") { this.takePhotoButton.IsEnabled = false; this.statusTextBlock.Text = "비디오 기록을 초기화 합니다."; this.videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(VIDEO_FILE_NAME, CreationCollisionOption.GenerateUniqueName); this.statusTextBlock.Text = "비디오 저장소 파일이 성공적으로 준비되었습니다."; MediaEncodingProfile mediaEncodingProfile = null; mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); await this.mediaCapture.StartRecordToStorageFileAsync(mediaEncodingProfile, this.videoStorageFile); this.recordVideoButton.IsEnabled = true; this.recordVideoButton.Content = "비디오 기록 중단"; this.isRecording = true; this.statusTextBlock.Text = "비디오 기록 진행중... 중단하기 위해 \'비디오 기록 중단\' 버튼을 누르시기 바랍니다."; } else { this.takePhotoButton.IsEnabled = true; this.statusTextBlock.Text = "비디오 기록 중단중..."; await this.mediaCapture.StopRecordAsync(); this.isRecording = false; IRandomAccessStreamWithContentType randomAccessStreamWithContentType = await this.videoStorageFile.OpenReadAsync(); this.lastRecordedVideoMediaElement.AutoPlay = true; this.lastRecordedVideoMediaElement.SetSource(randomAccessStreamWithContentType, this.videoStorageFile.FileType); this.lastRecordedVideoMediaElement.Play(); this.statusTextBlock.Text = "기록된 비디오 재생 : " + this.videoStorageFile.Path; this.recordVideoButton.Content = "비디오 기록 시작"; } } catch(Exception exception) { if(exception is UnauthorizedAccessException) { this.statusTextBlock.Text = "기록된 비디오를 재생할 수 없습니다; \'" + this.videoStorageFile.Path + "\'에 비디오가 성공적으로 기록되었습니다."; this.recordVideoButton.Content = "비디오 기록 시작"; } else { this.statusTextBlock.Text = exception.Message; ProcessCloseCamera(); } } finally { this.recordVideoButton.IsEnabled = true; } } #endregion #region 오디오 기록 버튼 클릭시 처리하기 - recordAudioButton_Click(sender, e) /// <summary> /// 오디오 기록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void recordAudioButton_Click(object sender, RoutedEventArgs e) { this.recordAudioButton.IsEnabled = false; this.playbackMediaElement.Source = null; try { if(this.recordAudioButton.Content.ToString() == "오디오 기록 시작") { this.audioStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(AUDIO_FILE_NAME, CreationCollisionOption.GenerateUniqueName); this.statusTextBlock.Text = "오디오 저장소 파일이 성공적으로 준비되었습니다."; MediaEncodingProfile mediaEncodingProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto); await this.mediaCapture.StartRecordToStorageFileAsync(mediaEncodingProfile, this.audioStorageFile); this.isRecording = true; this.recordAudioButton.IsEnabled = true; this.recordAudioButton.Content = "오디오 기록 중단"; this.statusTextBlock.Text = "오디오 기록 진행중... 중단하기 위해 \'오디오 기록 중단\' 버튼을 누르시기 바랍니다."; } else { this.statusTextBlock.Text = "오디오 기록 중단중..."; await this.mediaCapture.StopRecordAsync(); this.isRecording = false; this.recordAudioButton.IsEnabled = true; this.recordAudioButton.Content = "오디오 기록 시작"; IRandomAccessStream randomAccessStream = await this.audioStorageFile.OpenAsync(FileAccessMode.Read); this.statusTextBlock.Text = "기록된 오디오 재생 : " + this.audioStorageFile.Path; this.playbackMediaElement.AutoPlay = true; this.playbackMediaElement.SetSource(randomAccessStream, this.audioStorageFile.FileType); this.playbackMediaElement.Play(); } } catch(Exception exception) { this.statusTextBlock.Text = exception.Message; ProcessCloseCamera(); } finally { this.recordAudioButton.IsEnabled = true; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 초기화 버튼 표시 여부 설정하기 - SetInitializeButtonVisibility(action) /// <summary> /// 초기화 버튼 표시 여부 설정하기 /// </summary> /// <param name="action">액션</param> private void SetInitializeButtonVisibility(Action action) { if(action == Action.ENABLED) { this.initializeVideoButton.IsEnabled = true; this.initializeAudioButton.IsEnabled = true; } else { this.initializeVideoButton.IsEnabled = false; this.initializeAudioButton.IsEnabled = false; } } #endregion #region 비디오 버튼 표시 여부 설정하기 - SetVideoButtonVisibility(action) /// <summary> /// 비디오 버튼 표시 여부 설정하기 /// </summary> /// <param name="action">액션</param> private void SetVideoButtonVisibility(Action action) { if(action == Action.ENABLED) { this.takePhotoButton.IsEnabled = true; this.takePhotoButton.Visibility = Visibility.Visible; this.recordVideoButton.IsEnabled = true; this.recordVideoButton.Visibility = Visibility.Visible; } else { this.takePhotoButton.IsEnabled = false; this.takePhotoButton.Visibility = Visibility.Collapsed; this.recordVideoButton.IsEnabled = false; this.recordVideoButton.Visibility = Visibility.Collapsed; } } #endregion #region 오디오 버튼 표시 여부 설정하기 - SetAudioButtonVisibility(action) /// <summary> /// 오디오 버튼 표시 여부 설정하기 /// </summary> /// <param name="action">액션</param> private void SetAudioButtonVisibility(Action action) { if(action == Action.ENABLED) { this.recordAudioButton.IsEnabled = true; this.recordAudioButton.Visibility = Visibility.Visible; } else { this.recordAudioButton.IsEnabled = false; this.recordAudioButton.Visibility = Visibility.Collapsed; } } #endregion #region 카메라 닫기 처리하기 - ProcessCloseCamera() /// <summary> /// 카메라 닫기 처리하기 /// </summary> private async void ProcessCloseCamera() { if(this.mediaCapture != null) { if(this.isPreviewing) { await this.mediaCapture.StopPreviewAsync(); this.lastCapturedPhotoImage.Source = null; this.lastRecordedVideoMediaElement.Source = null; this.isPreviewing = false; } if(this.isRecording) { await this.mediaCapture.StopRecordAsync(); this.isRecording = false; this.recordVideoButton.Content = "비디오 기록 시작"; this.recordAudioButton.Content = "오디오 기록 시작"; } this.mediaCapture.Dispose(); this.mediaCapture = null; } SetInitializeButtonVisibility(Action.ENABLED); } #endregion } } |