첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ MP3 음악 파일을 재생하는 방법을 보여준다.

TestProject.zip
0.94MB

▶ 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="&#xf488;" />
                </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="&#xf478;" />
                </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="&#xf21a;" />
                </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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요