■ NAUDIO 라이브러리를 사용해 음악을 재생하는 방법을 보여준다.
[TestProject 프로젝트]
▶ 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 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:library="clr-namespace:TestLibrary;assembly=TestLibrary" Width="800" Height="600" Title="NAUDIO 라이브러리를 사용해 음악 재생하기"> <Grid Background="{DynamicResource WindowBackgroundBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="100" /> <RowDefinition Height="*" /> <RowDefinition Height="100" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="30*" /> <ColumnDefinition Width="60*" /> </Grid.ColumnDefinitions> <Menu Grid.Row="0" Grid.ColumnSpan="2"> <MenuItem Header="파일"> <MenuItem Name="openFileMenuItem" Header="파일 열기" /> <Separator /> <MenuItem Name="closeMenuItem" Header="종료" /> </MenuItem> <MenuItem Header="테마"> <MenuItem Name="defaultThemeMenuItem" Header="디폴트 (테마 없음)" IsCheckable="True" /> <MenuItem Name="expressionDarkThemeMenuItem" Header="다크 테마" IsCheckable="True" /> <MenuItem Name="expressionLightThemeMenuItem" Header="라이트 테마" IsCheckable="True" /> </MenuItem> </Menu> <library:DigitalClock x:Name="digitalClock" Grid.Row="1" Grid.ColumnSpan="2" Margin="5" HorizontalAlignment="Center" ShowHours="False" ShowSubSeconds="True" /> <library:AlbumArtDisplay x:Name="albumArtDisplay" Grid.Column="0" Grid.Row="2" Margin="5" /> <Grid Grid.Row="2" Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <library:SpectrumAnalyzer x:Name="spectrumAnalyzer" Margin="5" BarCount="16" /> <library:Equalizer x:Name="equalizer" Grid.Column="1" BandCount="7" /> </Grid> <Grid Grid.Row="3" Grid.ColumnSpan="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <library:WaveformTimeline x:Name="waveformTimeline" Margin="5" BorderThickness="1" /> <StackPanel Grid.Column="1" Orientation="Vertical" VerticalAlignment="Center"> <library:TimeEditor Name="repeatStartTimeEdit" Margin="5" IsReadOnly="True" /> <library:TimeEditor Name="repeatStopTimeEdit" Margin="5" IsReadOnly="True" /> </StackPanel> </Grid> <Grid Grid.Row="4" Grid.ColumnSpan="2" Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBox Name="filePathTextBox" Grid.Column="0" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Center" IsReadOnly="True" /> <StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal"> <Button Name="browseButton" Margin="2 2 10 2" HorizontalAlignment="Center" VerticalAlignment="Center" Height="24" Width="50" Content="선곡" /> <Button Name="playButton" Margin="2" Width="50" VerticalAlignment="Center" Height="24" IsEnabled="False" Content="재생" /> <Button Name="pauseButton" Margin="2" VerticalAlignment="Center" Width="50" Height="24" IsEnabled="False" Content="중지" /> <Button Name="stopButton" Margin="2" VerticalAlignment="Center" Width="50" Height="24" IsEnabled="False" Content="중단" /> </StackPanel> </Grid> </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 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 |
using Microsoft.Win32; using System; using System.ComponentModel; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.Windows.Data; using TestLibrary; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); NAudioEngine naudioEngine = NAudioEngine.Instance; UIHelper.Bind(naudioEngine, "CanStop" , this.stopButton , Button.IsEnabledProperty ); UIHelper.Bind(naudioEngine, "CanPlay" , this.playButton , Button.IsEnabledProperty ); UIHelper.Bind(naudioEngine, "CanPause" , this.pauseButton , Button.IsEnabledProperty ); UIHelper.Bind(naudioEngine, "SelectionBegin", this.repeatStartTimeEdit, TimeEditor.ValueProperty, BindingMode.TwoWay); UIHelper.Bind(naudioEngine, "SelectionEnd" , this.repeatStopTimeEdit , TimeEditor.ValueProperty, BindingMode.TwoWay); this.spectrumAnalyzer.RegisterSpectrumPlayer(naudioEngine); this.waveformTimeline.RegisterSoundPlayer(naudioEngine); LoadExpressionDarkTheme(); naudioEngine.PropertyChanged += naudioEngine_PropertyChanged; this.openFileMenuItem.Click += openFileMenuItem_Click; this.closeMenuItem.Click += closeMenuItem_Click; this.defaultThemeMenuItem.Checked += defaultThemeMenuItem_Checked; this.expressionDarkThemeMenuItem.Checked += expressionDarkThemeMenuItem_Checked; this.expressionLightThemeMenuItem.Checked += expressionLightThemeMenuItem_Checked; this.browseButton.Click += browseButton_Click; this.playButton.Click += playButton_Click; this.pauseButton.Click += pauseButton_Click; this.stopButton.Click += stopButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 닫을 경우 처리하기 - OnClosing(e) /// <summary> /// 닫을 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnClosing(CancelEventArgs e) { NAudioEngine.Instance.Dispose(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region NAUDIO 엔진 속성 변경시 처리하기 - naudioEngine_PropertyChanged(sender, e) /// <summary> /// NAUDIO 엔진 속성 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void naudioEngine_PropertyChanged(object sender, PropertyChangedEventArgs e) { NAudioEngine naudioEngine = NAudioEngine.Instance; switch(e.PropertyName) { case "FileTag" : if(naudioEngine.FileTag != null) { TagLib.Tag tag = naudioEngine.FileTag.Tag; if(tag.Pictures.Length > 0) { using(MemoryStream memoryStream = new MemoryStream(tag.Pictures[0].Data.Data)) { try { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); this.albumArtDisplay.AlbumArtImage = bitmapImage; } catch(NotSupportedException) { this.albumArtDisplay.AlbumArtImage = null; } memoryStream.Close(); } } else { this.albumArtDisplay.AlbumArtImage = null; } } else { this.albumArtDisplay.AlbumArtImage = null; } break; case "ChannelPosition" : this.digitalClock.Time = TimeSpan.FromSeconds(naudioEngine.ChannelPosition); break; default: break; } } #endregion #region 파일 열기 메뉴 항목 클릭시 처리하기 - openFileMenuItem_Click(sender, e) /// <summary> /// 파일 열기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openFileMenuItem_Click(object sender, RoutedEventArgs e) { OpenFile(); } #endregion #region 닫기 메뉴 항목 클릭시 처리하기 - closeMenuItem_Click(sender, e) /// <summary> /// 닫기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeMenuItem_Click(object sender, RoutedEventArgs e) { Close(); } #endregion #region 디폴트 테마 메뉴 항목 클릭시 처리하기 - defaultThemeMenuItem_Checked(sender, e) /// <summary> /// 디폴트 테마 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void defaultThemeMenuItem_Checked(object sender, RoutedEventArgs e) { LoadDefaultTheme(); } #endregion #region 익스프레션 다크 테마 메뉴 항목 클릭시 처리하기 - expressionDarkThemeMenuItem_Checked(sender, e) /// <summary> /// 익스프레션 다크 테마 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void expressionDarkThemeMenuItem_Checked(object sender, RoutedEventArgs e) { LoadExpressionDarkTheme(); } #endregion #region 익스프레션 라이트 테마 메뉴 항목 클릭시 처리하기 - expressionLightThemeMenuItem_Checked(sender, e) /// <summary> /// 익스프레션 라이트 테마 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void expressionLightThemeMenuItem_Checked(object sender, RoutedEventArgs e) { LoadExpressionLightTheme(); } #endregion #region 선곡 버튼 클릭시 처리하기 - browseButton_Click(sender, e) /// <summary> /// 선곡 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void browseButton_Click(object sender, RoutedEventArgs e) { OpenFile(); } #endregion #region 재생 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// 재생 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, RoutedEventArgs e) { if(NAudioEngine.Instance.CanPlay) { NAudioEngine.Instance.Play(); } } #endregion #region 중지 버튼 클릭시 처리하기 - pauseButton_Click(sender, e) /// <summary> /// 중지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pauseButton_Click(object sender, RoutedEventArgs e) { if(NAudioEngine.Instance.CanPause) { NAudioEngine.Instance.Pause(); } } #endregion #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 중단 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, RoutedEventArgs e) { if(NAudioEngine.Instance.CanStop) { NAudioEngine.Instance.Stop(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Event #region 파일 열기 - OpenFile() /// <summary> /// 파일 열기 /// </summary> private void OpenFile() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "MP3 파일|*.mp3"; if(openFileDialog.ShowDialog() == true) { NAudioEngine.Instance.OpenFile(openFileDialog.FileName); this.filePathTextBox.Text = openFileDialog.FileName; } } #endregion #region 디폴트 테마 로드하기 - LoadDefaultTheme() /// <summary> /// 디폴트 테마 로드하기 /// </summary> private void LoadDefaultTheme() { this.defaultThemeMenuItem.IsChecked = true; this.defaultThemeMenuItem.IsEnabled = false; this.expressionDarkThemeMenuItem.IsChecked = false; this.expressionDarkThemeMenuItem.IsEnabled = true; this.expressionLightThemeMenuItem.IsChecked = false; this.expressionLightThemeMenuItem.IsEnabled = true; Resources.MergedDictionaries.Clear(); } #endregion #region 익스프레션 다크 테마 로드하기 - LoadExpressionDarkTheme() /// <summary> /// 익스프레션 다크 테마 로드하기 /// </summary> private void LoadExpressionDarkTheme() { this.defaultThemeMenuItem.IsChecked = false; this.defaultThemeMenuItem.IsEnabled = true; this.expressionDarkThemeMenuItem.IsChecked = true; this.expressionDarkThemeMenuItem.IsEnabled = false; this.expressionLightThemeMenuItem.IsChecked = false; this.expressionLightThemeMenuItem.IsEnabled = true; Resources.MergedDictionaries.Clear(); ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri("ExpressionDark.xaml", UriKind.Relative)) as ResourceDictionary; Resources.MergedDictionaries.Add(resourceDictionary); } #endregion #region 익스프레션 라이트 테마 로드하기 - LoadExpressionLightTheme() /// <summary> /// 익스프레션 라이트 테마 로드하기 /// </summary> private void LoadExpressionLightTheme() { this.defaultThemeMenuItem.IsChecked = false; this.defaultThemeMenuItem.IsEnabled = true; this.expressionDarkThemeMenuItem.IsChecked = false; this.expressionDarkThemeMenuItem.IsEnabled = true; this.expressionLightThemeMenuItem.IsChecked = true; this.expressionLightThemeMenuItem.IsEnabled = false; Resources.MergedDictionaries.Clear(); ResourceDictionary themeResources = Application.LoadComponent(new Uri("ExpressionLight.xaml", UriKind.Relative)) as ResourceDictionary; Resources.MergedDictionaries.Add(themeResources); } #endregion } } |
※ 컴파일시 TestProject 프로젝트의 플랫폼 대상을 x86으로 설정해야 한다.