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.
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:
var builder = MauiApp.CreateBuilder();
// Initialize the .NET MAUI Community Toolkit MediaElement by adding the below line of code
// After initializing the .NET MAUI Community Toolkit, optionally add additional fonts, and other things
How can I initialize MediaElement in OnCreate?
protected override void OnCreate(Bundle bundle)
_game = new Game1();
_view = _game.Services.GetService(typeof(View)) as View;
How can I initialize MediaElement in FinishedLaunching?
public override void FinishedLaunching(UIApplication app)
I want to use MediaElement to play background songs in my shared code project after initializing it in OnCreate/FinishedLaunching.