How can I use MediaElement to play audio from file in a .NET iOS/Android project?

I don't know how to play audio from file with MediaElement. I want to play background songs in my game. I don't need to display additional buttons in my game to start or stop playing the songs. My game will start playing the songs automatically when the player enters a menu in my game. Normally the songs should repeat automatically, but in some cases I just want to play a song once. In addition, sometimes I will need to start playing a song not from the beginning but from a specific position.
On iOS the audio .m4a files are in Resources folder. On Android the audio .m4a files are in Assets folder.
Android csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-android33.0</TargetFramework>
<SupportedOSPlatformVersion>31.0</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<UseMaui>true</UseMaui>
<UseMauiEssentials>true</UseMauiEssentials>
iOS csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-ios16.1</TargetFramework>
<OutputType>Exe</OutputType>
<UseMaui>true</UseMaui>
<UseMauiEssentials>true</UseMauiEssentials>
<SupportedOSPlatformVersion>15.0</SupportedOSPlatformVersion>
In addition, I cannot use the following code to initialize MediaElement because I don't use the Maui template in Visual Studio for Mac to create my Android and iOS projects:
https://devblogs.microsoft.com/dotnet/announcing-dotnet-maui-communitytoolkit-mediaelement/
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
// Initialize the .NET MAUI Community Toolkit MediaElement by adding the below line of code
.UseMauiCommunityToolkitMediaElement()
// After initializing the .NET MAUI Community Toolkit, optionally add additional fonts, and other things
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
How can I initialize MediaElement in OnCreate?
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_game = new Game1();
_view = _game.Services.GetService(typeof(View)) as View;
SetContentView(_view);
_game.Run();
}
How can I initialize MediaElement in FinishedLaunching?
public override void FinishedLaunching(UIApplication app)
{
RunGame();
}
I want to use MediaElement to play background songs in my shared code project after initializing it in OnCreate/FinishedLaunching.