Search code examples
c#audiomonogame

Hold list of currently active SoundEffectInstances


I'm currently trying to make a SoundManager class for my game. It's supposed to hold a list of all currently playing SoundEffectInstances, so I can loop this list to pause (and resume) all Sounds, for example when the game is paused.

My Play function is pretty straight forward:

    public static void PlaySound(string name)
    {
        sTestSoundInstance = sTestSound.CreateInstance();
        sAllSounds.Add(sTestSoundInstance);
        sTestSoundInstance.Play();
    }

However, I can't wrap my head around how I can check if a sound has finished playing, and when I can remove it from sAllSounds. Please consider there will be different Sounds of different leghts. Any thought on this is apprechiated.


Solution

  • I avoided this problem by making my own sound class: It gets initialized with a SoundEffect, stores the duration property, creates a SoundEffectInstance and drops the SoundEffect. The SoundManager class holds a list of these new SoundEffect class. Each Instance gets updated every Frame, and sets a flag it the duration has been reached. The SoundManager delets the Instance from the list within the next frame.