/XamarinAudioManager

Cross platform Audio Manager for Xamarin

Primary LanguageC#MIT LicenseMIT

XamarinAudioManager

standard-readme compliant

banner

Cross platform Audio Manager for Xamarin Forms

Xamarin Audio Manager provides a simple way to play audio files in Xamarin Forms projects.

Note: I have not been developing for Xamarin lately and have been to busy to give any time this project. If some one wants to take it over let me know.

The Supported Xamarin Platforms are:

  • iOS
  • Android
  • UWP
  • Windows 8.1
  • Windows Phone 8.1 //Sorta needs work but how many people use win phone ;)

Here is a sample showing how you can use Xamarin Audio Manager to set background music and play an effect sound.

await Audio.Manager.PlayBackgroundMusic("bgMusic.mp3");

await Audio.Manager.PlaySound("Drop.mp3");

Table of Contents

Install

Install the XamarinAudioManager NuGet Package.

If you reference the package from a Xamarin Portable project, you will also need to reference the package from each Xamarin platform specific project. This is because the Xamarin Portable version of Xamarin Audio Manager doesn't contain the actual implementation of the audio APIs (because it differs from platform to platform), so referencing the package from a platform specific project will ensure that Xamarin Audio Manager is included in the app and used at runtime.

The target platforms need to initalize the AudioManager or the dll will be removed on compile. For iOS and Adndroind ths is done by calling a static class Initializer.Initialize();. For Windows platforms you need to add a ref to the base canvas so the volume and mute fucntions will work so we are going to implement an interface 'IAudioManagerContainer' to pass in that ref and initalize dll.

iOS

Add Initializer.Initialize(); to Main.cs. See exaple below.

static void Main(string[] args)
{
    // if you want to use a different Application Delegate class from "AppDelegate"
    // you can specify it here.
    UIApplication.Main(args, null, "AppDelegate");

    Initializer.Initialize();
}

Android

Add Initializer.Initialize(); to MainActivity.cs. See exaple below.

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(bundle);

    global::Xamarin.Forms.Forms.Init(this, bundle);
    LoadApplication(new App());

    Initializer.Initialize();
}

UWP

Windows81

On Windows platforms we need to implement an interface 'IAudioManagerContainer' in the MainPage.xaml.cs. We will use this interface to pass a ref to the base canvas. See example below.

public sealed partial class MainPage : IAudioManagerContainer
{
    public MainPage()
    {
        this.InitializeComponent();

        LoadApplication(new XamarinAudioManagerTest.App());

        AudioManagerContainer = this.Content as Canvas;
    }

    public Canvas AudioManagerContainer { get; set; }
}

WindowsPhone81

Windows Phone is problimatic. for now just modify the MainPage.xaml.cs like below. Audio will play but Volume and mute will not work.

public sealed partial class MainPage : IAudioManagerContainer
{
    public Canvas AudioManagerContainer { get; set; } = new Canvas();

Usage

Create a folder in each target platform to store the sounds files the default is 'Sounds' but if you want something diffent set Audio.Manager.SoundPath = [TargetFolderName]. An alternitive is to add the files to a common shared folder and link to the sound files. Click here for more info on file linking.

//Play a mp3 on loop as the background music.
 await Audio.Manager.PlayBackgroundMusic("bgMusic.mp3");
 
 //Set or Get the state of the background music.
 Audio.Manager.MusicOn = True;
 
 //Set the volume level of the background music from 0 to 1.
 Audio.Manager.BackgroundMusicVolume = 0.5;
 
  //Set or Get the state of the Effect sounds.
 Audio.Manager.EffectsOn = True;
 
  //Set the volume level of the Effects from 0 to 1.
 Audio.Manager.EffectsVolume = 0.5;
 
 //Play an effect sound. On Android the lenth is limeted to 5 seconds.
 await Audio.Manager.PlaySound("Drop.mp3"); 

Contribute

I have not been developing for Xamarin lately and have been to busy to give any time this project. If some one wants to take it over let me know.

Feel free to help out! Open an issue or submit PRs.

Xamarin Audio Manager follows the Contributor Covenant Code of Conduct.

License

MIT © John Cutburth II