728x90
728x170
1. "Plugin.Maui.Audio" Nuget Pakcage Install
- 시험판 포함 체크
2. mp3 파일 넣기
Resources > Raw 폴더에 mp3 파일을 넣는다.
3. 화면 정의 및 ViewModel 코딩
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Maui.AudioTest.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
BackgroundColor="White">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
IsVisible="{Binding IsLoadComplete}"
Spacing="25"
VerticalOptions="Center">
<Label
HorizontalOptions="Center"
Text="Audio Test"
TextColor="Black" />
<Label
HorizontalOptions="Center"
Text="{Binding Status, StringFormat=[ {0} ]}"
TextColor="Black" />
<HorizontalStackLayout HorizontalOptions="Center" Spacing="15">
<Button
BackgroundColor="Black"
Command="{Binding PlayCommand}"
HeightRequest="50"
SemanticProperties.Hint="Start playing"
Text="▶"
TextColor="White"
WidthRequest="50" />
<Button
BackgroundColor="Black"
Command="{Binding PauseCommand}"
FontAttributes="Bold"
HeightRequest="50"
SemanticProperties.Hint="Pause playing"
Text="||"
TextColor="White"
WidthRequest="50" />
<Button
BackgroundColor="Black"
Command="{Binding StopCommand}"
HeightRequest="50"
SemanticProperties.Hint="Stop playing"
Text="■"
TextColor="White"
WidthRequest="50" />
</HorizontalStackLayout>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
MainPage.xaml.cs
using Maui.AudioTest.ViewModels;
namespace Maui.AudioTest
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}
MainViewModel.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Plugin.Maui.Audio;
namespace Maui.AudioTest.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
#region Fields
private IAudioManager audioManager;
private IAudioPlayer audioPlayer;
private bool isLoadComplete = false;
public bool IsLoadComplete
{
get
{
return this.isLoadComplete;
}
set
{
this.isLoadComplete = value;
NotifyPropertyChanged();
}
}
private string status = "Wait";
public string Status
{
get
{
return this.status;
}
set
{
this.status = value;
NotifyPropertyChanged();
}
}
public Command PlayCommand { get; set; }
public Command PauseCommand { get; set; }
public Command StopCommand { get; set; }
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
// Constructor
#region MainViewModel
public MainViewModel()
{
this.audioManager = new AudioManager();
SetMusic();
PlayCommand = new Command(OnPlayCommand);
PauseCommand = new Command(OnPauseCommand);
StopCommand = new Command(OnStopCommand);
}
#endregion
#region SetMusic
/// <summary>
/// Play 될 음악 파일을 설정합니다.
/// </summary>
private async void SetMusic()
{
audioPlayer = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("motive.mp3"));
if (audioPlayer != null)
{
IsLoadComplete = true;
}
}
#endregion
#region OnPlayCommand
void OnPlayCommand()
{
audioPlayer.Play();
Status = "Play";
}
#endregion
#region OnPauseCommand
void OnPauseCommand()
{
if (audioPlayer.IsPlaying)
{
audioPlayer.Pause();
Status = "Pause";
}
else
{
audioPlayer.Play();
Status = "Play";
}
}
#endregion
#region OnStopCommand
void OnStopCommand()
{
if (audioPlayer.IsPlaying)
{
audioPlayer.Stop();
Status = "Stop";
}
}
#endregion
}
}
실행영상
728x90
그리드형
'C# > Xamarin Maui' 카테고리의 다른 글
[.NET MAUI] CommunityToolkit.Maui Snackbar (0) | 2022.09.14 |
---|---|
[.NET MAUI] Open Navigate/Drive Map (0) | 2022.09.05 |
[.NET MAUI] AdMob 광고 적용하기 (전면,배너,보상) (2) | 2022.08.22 |
[.NET MAUI] 테스트 AdMob ID (0) | 2022.08.21 |
[.NET MAUI] BarCode/QRCode 사용하기 (ZXing) (6) | 2022.08.19 |