[C#/MAUI/.NET6] MP3 음악 파일 재생하기
■ MP3 음악 파일을 재생하는 방법을 보여준다. ▶ 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 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="30"> <HorizontalStackLayout HorizontalOptions="Center" Spacing="30"> <Image> <Image.Source> <FontImageSource x:Name="playFontImageSource" FontFamily="ionicons" Size="48" Color="Black" Glyph="" /> </Image.Source> <Image.GestureRecognizers> <TapGestureRecognizer x:Name="playTapGestureRecognizer" NumberOfTapsRequired="1" /> </Image.GestureRecognizers> </Image> <Image> <Image.Source> <FontImageSource x:Name="pauseFontImageSource" FontFamily="ionicons" Size="48" Color="Gray" Glyph="" /> </Image.Source> <Image.GestureRecognizers> <TapGestureRecognizer x:Name="pauseTapGestureRecognizer" NumberOfTapsRequired="1" /> </Image.GestureRecognizers> </Image> <Image> <Image.Source> <FontImageSource x:Name="stopFontImageSource" FontFamily="ionicons" Size="48" Color="Gray" Glyph="" /> </Image.Source> <Image.GestureRecognizers> <TapGestureRecognizer x:Name="stopTapGestureRecognizer" NumberOfTapsRequired="1" /> </Image.GestureRecognizers> </Image> </HorizontalStackLayout> <Slider x:Name="volumeSlider" WidthRequest="300" Minimum="0" Maximum="1" Value="0.3"/> </VerticalStackLayout> </ContentPage> |
▶ 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 |
using Plugin.Maui.Audio; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 오디오 관리자 /// </summary> private IAudioManager audioManager; /// <summary> /// 오디오 재생자 /// </summary> private IAudioPlayer audioPlayer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.audioManager = new AudioManager(); Loaded += ContentPage_Loaded; this.playTapGestureRecognizer.Tapped += playTapGestureRecognizer_Tapped; this.volumeSlider.ValueChanged += volumeSlider_ValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 컨텐트 페이지 로드시 처리하기 - ContentPage_Loaded(sender, e) /// <summary> /// 컨텐트 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void ContentPage_Loaded(object sender, EventArgs e) { this.audioPlayer = this.audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("sample.mp3")); this.audioPlayer.Volume = this.volumeSlider.Value; this.audioPlayer.PlaybackEnded += audioPlayer_PlaybackEnded; } #endregion #region 오디오 재생자 재생 종료시 처리하기 - audioPlayer_PlaybackEnded(sender, e) /// <summary> /// 오디오 재생자 재생 종료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void audioPlayer_PlaybackEnded(object sender, EventArgs e) { this.playTapGestureRecognizer.Tapped -= playTapGestureRecognizer_Tapped; this.pauseTapGestureRecognizer.Tapped -= pauseTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped -= stopTapGestureRecognizer_Tapped; this.playTapGestureRecognizer.Tapped += playTapGestureRecognizer_Tapped; this.playFontImageSource.Color = Colors.Black; this.pauseFontImageSource.Color = Colors.Gray; this.stopFontImageSource.Color = Colors.Gray; } #endregion #region 재생 탭 제스처 인식기 탭 처리하기 - playTapGestureRecognizer_Tapped(sender, e) /// <summary> /// 재생 탭 제스처 인식기 탭 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playTapGestureRecognizer_Tapped(object sender, EventArgs e) { this.playTapGestureRecognizer.Tapped -= playTapGestureRecognizer_Tapped; this.pauseTapGestureRecognizer.Tapped -= pauseTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped -= stopTapGestureRecognizer_Tapped; this.pauseTapGestureRecognizer.Tapped += pauseTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped += stopTapGestureRecognizer_Tapped; this.playFontImageSource.Color = Colors.Gray; this.pauseFontImageSource.Color = Colors.Black; this.stopFontImageSource.Color = Colors.Black; this.audioPlayer.Play(); } #endregion #region 중지 탭 제스처 인식기 탭 처리하기 - pauseTapGestureRecognizer_Tapped(sender, e) /// <summary> /// 중지 탭 제스처 인식기 탭 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pauseTapGestureRecognizer_Tapped(object sender, EventArgs e) { this.playTapGestureRecognizer.Tapped -= playTapGestureRecognizer_Tapped; this.pauseTapGestureRecognizer.Tapped -= pauseTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped -= stopTapGestureRecognizer_Tapped; this.playTapGestureRecognizer.Tapped += playTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped += stopTapGestureRecognizer_Tapped; this.playFontImageSource.Color = Colors.Black; this.pauseFontImageSource.Color = Colors.Gray; this.stopFontImageSource.Color = Colors.Black; this.audioPlayer.Pause(); } #endregion #region 중단 탭 제스처 인식기 탭 처리하기 - stopTapGestureRecognizer_Tapped(sender, e) /// <summary> /// 중단 탭 제스처 인식기 탭 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopTapGestureRecognizer_Tapped(object sender, EventArgs e) { this.playTapGestureRecognizer.Tapped -= playTapGestureRecognizer_Tapped; this.pauseTapGestureRecognizer.Tapped -= pauseTapGestureRecognizer_Tapped; this.stopTapGestureRecognizer.Tapped -= stopTapGestureRecognizer_Tapped; this.playTapGestureRecognizer.Tapped += playTapGestureRecognizer_Tapped; this.playFontImageSource.Color = Colors.Black; this.pauseFontImageSource.Color = Colors.Gray; this.stopFontImageSource.Color = Colors.Gray; this.audioPlayer.Stop(); } #endregion #region 볼륨 슬라이더 값 변경시 처리하기 - volumeSlider_ValueChanged(sender, e) /// <summary> /// 볼륨 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void volumeSlider_ValueChanged(object sender, ValueChangedEventArgs e) { this.audioPlayer.Volume = this.volumeSlider.Value; } #endregion } |
TestProject.zip