jfversluis / Plugin.Maui.Audio

Plugin.Maui.Audio provides the ability to play audio inside a .NET MAUI application
MIT License
263 stars 46 forks source link

40 players max on Android #139

Open Tsayper opened 3 weeks ago

Tsayper commented 3 weeks ago

I'm creating an Android app which should play many different sounds, and using Plugin.Maui.Audio for this purpose. The problem is, when i load a 41st sound, it wont play. No exception is thrown, just silence. Code:

public static class Helper
{
    public static Dictionary<string, AsyncAudioPlayer> players = [];

    public static async Task PlaySound(string s)
    {
        if (!players.ContainsKey(s))
        {
            var snd = Assembly.GetExecutingAssembly().GetManifestResourceStream("Chitaika.Resources.Sounds." + s + ".wav");
            var p = AudioManager.Current.CreateAsyncPlayer(snd!);
            players.Add(s, p);
        }
        await players[s].PlayAsync(new CancellationToken());
    }
}
bijington commented 3 weeks ago

That is odd. Just to rule some things out... is it the same file that is failing? If you move that to the first sound played does it still fail?

Tsayper commented 3 weeks ago

That is odd. Just to rule some things out... is it the same file that is failing? If you move that to the first sound played does it still fail?

no, sounds are played in semi-random order (dependent on the user's actions). And 41th player onwards dont play sound (return immediately after await PlayAsync is called), while the first 40 players continue to work.

bijington commented 3 weeks ago

If you stick a try catch around the call to PlayAsync and stick a breakpoint in the catch does it tell you anything?

ewerspej commented 2 weeks ago

@Tsayper I would expect this to be related to resource starvation. 40 players means 40 handles to unmanaged resources.

Have you tried disposing of the player instance after playing a sound and only keeping a single player reference like I suggested in my answer to your Stack Overflow question on this issue?

bijington commented 1 week ago

@ewerspej that is a very good point! If this is happening on Android hopefully you might see some vaguely helpful ADB log entries confirming that some resources couldn't be allocated