Code Monkey home page Code Monkey logo

xamarinaudiomanager's Introduction

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.

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

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

xamarinaudiomanager's People

Contributors

jcphlux avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.