■ ElementSoundPlayer 클래스의 State/SpatialAudioMode 정적 속성과 Play 정적 메소드를 사용하는 방법을 보여준다.
▶ 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 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <ToggleSwitch Name="soundToggleSwitch" HorizontalAlignment="Center" OffContent="Sound Off" OnContent="Sound On" /> <StackPanel Margin="0 30 0 0" Orientation="Vertical"> <CheckBox Name="spatialAudioCheckBox" IsEnabled="False" Content="Enable Spatial Audio" /> <TextBlock Margin="0 10 0 0" Text="Can only enable spatial audio when sound is on!" Foreground="{ThemeResource SystemColorHotlightColor}" Style="{ThemeResource CaptionTextBlockStyle}" FontStyle="Italic" /> </StackPanel> <Grid Margin="0 30 0 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Name="playSoundButton" Grid.Column="0" Content="Play Sound" ElementSoundMode="Off" HorizontalAlignment="Left" /> <ComboBox Name="soundSelectionComboBox" Grid.Column="1" Header="Pick Custom Sound" SelectedIndex="1" HorizontalAlignment="Right" Margin="50 0 0 0"> <ComboBoxItem>Focus</ComboBoxItem> <ComboBoxItem>Invoke</ComboBoxItem> <ComboBoxItem>Show</ComboBoxItem> <ComboBoxItem>Hide</ComboBoxItem> <ComboBoxItem>MoveNext</ComboBoxItem> <ComboBoxItem>MovePrevious</ComboBoxItem> <ComboBoxItem>GoBack</ComboBoxItem> </ComboBox> </Grid> </StackPanel> </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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.soundToggleSwitch.Toggled += soundToggleSwitch_Toggled; this.spatialAudioCheckBox.Checked += enableSpatialAudioCheckBox_Checked; this.spatialAudioCheckBox.Unchecked += spatialAudioCheckBox_Unchecked; this.playSoundButton.Click += playSoundButton_Click; if(ElementSoundPlayer.State == ElementSoundPlayerState.On) { this.soundToggleSwitch.IsOn = true; } if(ElementSoundPlayer.SpatialAudioMode == ElementSpatialAudioMode.On && soundToggleSwitch.IsOn == true) { this.spatialAudioCheckBox.IsChecked = true; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Sound 토글 스위치 토글시 처리하기 - soundToggleSwitch_Toggled(sender, e) /// <summary> /// Sound 토글 스위치 토글시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void soundToggleSwitch_Toggled(object sender, RoutedEventArgs e) { if(this.soundToggleSwitch.IsOn == true) { this.spatialAudioCheckBox.IsEnabled = true; ElementSoundPlayer.State = ElementSoundPlayerState.On; } else { this.spatialAudioCheckBox.IsEnabled = false; this.spatialAudioCheckBox.IsChecked = false; ElementSoundPlayer.State = ElementSoundPlayerState.Off; ElementSoundPlayer.SpatialAudioMode = ElementSpatialAudioMode.Off; } } #endregion #region Enable Spatial Audio 체크 박스 체크시 처리하기 - enableSpatialAudioCheckBox_Checked(sender, e) /// <summary> /// Enable Spatial Audio 체크 박스 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void enableSpatialAudioCheckBox_Checked(object sender, RoutedEventArgs e) { if(this.soundToggleSwitch.IsOn == true) { ElementSoundPlayer.SpatialAudioMode = ElementSpatialAudioMode.On; } } #endregion #region Enable Spatial Audio 체크 박스 체크 해제시 처리하기 - spatialAudioCheckBox_Unchecked(sender, e) /// <summary> /// Enable Spatial Audio 체크 박스 체크 해제시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void spatialAudioCheckBox_Unchecked(object sender, RoutedEventArgs e) { if(this.soundToggleSwitch.IsOn == true) { ElementSoundPlayer.SpatialAudioMode = ElementSpatialAudioMode.Off; } } #endregion #region Play Sound 버튼 클릭시 처리하기 - playSoundButton_Click(sender, e) /// <summary> /// Play Sound 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playSoundButton_Click(object sender, RoutedEventArgs e) { ElementSoundPlayer.Play((ElementSoundKind)this.soundSelectionComboBox.SelectedIndex); } #endregion } } |