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

Kim Strasser 661 Reputation points
2023-03-16T09:17:48+00:00

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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,867 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Kim Strasser 661 Reputation points
    2023-03-29T01:24:49.97+00:00

    I will continue using Xamarin MediaManager Plugin because I can use it in my iOS and Android projects. I thought that I could replace MediaManager with MediaElement.

    https://github.com/Baseflow/XamarinMediaManager

    0 comments No comments