Playing audio in a .NET MAUI app

The MAUI plugin for playing audio just got a stable release (v1.1.0), and with it you can play sounds and music in your .NET MAUI app. This short and sweet guide will show you how you can easily get started.

File -> New Project

Create a new .NET MAUI project. For target framework, you can choose .NET 6 or .NET 7 as the plugin supports both. Install the package Plugin.Maui.Audio, as shown in the screenshot below:

Then, add the audio file that you want to play to your project. Add it under the Resources/Raw folder. Remove the lines that was automatically added to your csproj file, as there already exists a config in your csproj that builds all assets in this folder as MauiAssets.

In this example, we’ll edit the click event for the button provided by the template to play our audio file on click:

private async void OnCounterClicked(object sender, EventArgs e)
{
    var audioPlayer = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ukelele.mp3"));

    audioPlayer.Play();
}

You can use the AudioManager.Current directly or you can use it through dependency injection. You can also access properties like IsPlaying, Duration and CurrentPosition. Check out the readme on the GitHub page for the plugin for more info.

Thanks to Gerald Versluis, Shaun Lawrence and the other contributors for creating this package!

1 thought on “Playing audio in a .NET MAUI app”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.