Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension Methods for ISimpleAudioPlayer (FadeIn/Out extension methods) #60

Open
terinfire opened this issue Oct 11, 2019 · 0 comments
Open

Comments

@terinfire
Copy link
Contributor

terinfire commented Oct 11, 2019

Sorry, I didn't fork -- but I came up with a way that should give you a little more feature-rich behavior, without having to go and implement it like crazy:

using Plugin.SimpleAudioPlayer;
using System.Threading;
using System.Threading.Tasks;

namespace Plugins.SimpleAudioPlayer.Extensions
{
    public static class ISimpleAudioPlayerExtensions
    {
        public static void FadeIn(this ISimpleAudioPlayer player, int milliseconds)
        {
            if (player == null || player.IsPlaying || milliseconds < 0) return;

            Task.Factory.StartNew(() =>
            {
                var step = milliseconds / 100;

                player.Volume = 0d;

                if (!player.IsPlaying)
                {
                    player.Play();
                }

                for (var i = 1; i <= 100; i++)
                {
                    player.Volume = (.01d * i);
                    Thread.Sleep(step);
                }
            });
        }

        public static void FadeOut(this ISimpleAudioPlayer player, int milliseconds)
        {
            if (player == null || !player.IsPlaying || milliseconds < 0) return;

            Task.Factory.StartNew(() =>
            {
                var step = milliseconds / 100;

                for (var i = 0; i < 100; i++)
                {
                    player.Volume = 1d - (.01d * i);
                    Thread.Sleep(step);
                }

                player.Stop();
                player.Volume = 1d;
            });
        }
    }
}

I've tested this in iOS and Android -- and it works REALLY well in both. Hopefully it will be useful to others (or you can incorporate it into the mainline -- my thought would be to just add an "ISimpleAudioPlayerExtensions.cs" file in the same directory as the ISimpleAudioPlayer definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant