Plugin.Maui.Audio
provides the ability to play, record and stream audio inside a .NET MAUI application.
- Available on NuGet: http://www.nuget.org/packages/Plugin.Maui.Audio
Plugin.Maui.Audio
provides the AudioManager
class that allows for the creation of AudioPlayers
and AudioRecorders
and AudioStreamers
. The AudioManager
can be used with or without dependency injection.
There are two different ways in which you can interact with the AudioManager
implementation provided by this plugin, they are:
You will first need to register the AudioManager
with the MauiAppBuilder
based on the following example:
builder.AddAudio();
You can then enable your classes to depend on IAudioManager
as per the following example.
public class AudioPlayerViewModel
{
readonly IAudioManager audioManager;
public AudioPlayerViewModel(IAudioManager audioManager)
{
this.audioManager = audioManager;
}
public async Task PlayAudioAsync()
{
var audioPlayer = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ukelele.mp3"));
audioPlayer.Play();
}
}
Alternatively if you want to skip using the dependency injection approach you can use the AudioManager.Current
property.
public class AudioPlayerViewModel
{
public async Task PlayAudioAsync()
{
var audioPlayer = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ukelele.mp3"));
audioPlayer.Play();
}
}
Now that you know how to use the AudioManager
class, please refer to the following sections:
The audio formats supported for playback depend on the underlying platform:
Platform | Supported Formats |
---|---|
Android | MP3, WAV, AAC, FLAC, OGG and other formats supported by Android's MediaPlayer |
iOS/MacCatalyst | MP3, WAV, AAC, ALAC, AIFF, and other formats supported by AVAudioPlayer |
Windows | MP3, WAV, AAC, FLAC, WMA, and other formats supported by Windows MediaPlayer |
The audio file format is determined automatically based on the file extension or stream content.
Recording capabilities vary by platform with the following supported encoding formats:
Platform | Supported Recording Formats |
---|---|
Android | WAV (PCM), AAC (Android 12+) - MediaRecorder docs |
iOS/MacCatalyst | WAV (PCM), ULaw, ALAC (Apple Lossless), FLAC, AAC - AVAudioRecorder docs |
Windows | WAV (PCM), ALAC (Apple Lossless), FLAC, AAC - Windows.Media.Capture docs |
When recording or streaming PCM audio (WAV format):
- Sample Rates:
- Android: 44100Hz is recommended and most widely supported
- iOS/macOS: 48000Hz is recommended
- Customizable in all platforms (8000Hz, 16000Hz, etc.)
- Channels:
- Mono (1 channel) and Stereo (2 channels) supported on all platforms
- Mono is recommended for maximum compatibility
- Bit Depth:
- 8-bit and 16-bit PCM supported (16-bit recommended)
- Windows streaming supports only 16-bit PCM
This project could not have came to be without these projects and people, thank you! <3
Basically this plugin, but then for Xamarin. We have been using this in our Xamarin projects with much joy and ease, so thank you so much Adrian (and contributors!) for that. Find the original project here where we have based our project on and evolved it from there.
As a little sample song we wanted something Hawaii/Maui themed obviously, and we found The Happy Ukelele Song which seems to fit that description. Thank you Stanislav Fomin and AudioHero for making it available.