728x90
반응형
728x170
■ MP3 음악 파일을 재생하는 방법을 보여준다.
▶ MainPage.xaml
<?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
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
}
728x90
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] WindowExtensions 클래스 : SetDefaultStatusBarAppearance/SetDefaultNavigationBarAppearance 확장 메소드를 사용해 상태바/액션바 색상 설정하기 (ANDROID) (0) | 2022.09.29 |
---|---|
[C#/MAUI/.NET6] WindowExtensions 클래스 : DisplayContentBehindBars 확장 메소드를 사용해 상태바/액션바를 컨텐츠 영역 위로 올리기 (0) | 2022.09.29 |
[C#/MAUI/.NET6] ContentButton 엘리먼트 사용하기 (0) | 2022.09.29 |
[C#/MAUI/.NET6] Icon 엘리먼트 : TintColor 속성을 사용해 아이콘 색상 설정하기 (0) | 2022.09.29 |
[C#/MAUI/.NET6] 누겟 설치 : SimpleToolkit.Core (0) | 2022.09.29 |
[C#/MAUI/.NET6] 누겟 설치 : Plugin.Maui.Audio (0) | 2022.08.25 |
[C#/MAUI/.NET6] IMTAdmob 인터페이스 : 애드몹(AdMob) 전면 광고 사용하기 (0) | 2022.08.21 |
[C#/MAUI/.NET6] MTAdView 엘리먼트 : 애드몹(AdMob) 배너 광고 사용하기 (0) | 2022.08.21 |
[C#/MAUI/.NET6] 누겟 설치 : Plugin.MauiMTAdmob (0) | 2022.08.21 |
[C#/MAUI/.NET6] CameraBarcodeReaderView 엘리먼트 : 바코드/QR 코드 캡처하기 (0) | 2022.08.21 |
댓글을 달아 주세요